Error loading HTTP application in Delphi when uploading files to streams

I am working on an HTTP web server (via TIdHTTPWebBrokerBridge ) and have a little problem when it comes to uploading files to streams ( TFileStream ) that will be sent back to the client. This happens not every time, but in a rather random way ... I get an exception ...

Cannot open file "C:\SomePath\SomeFile.html". The process cannot access the file because it is being used by another process

This happens on this line:

Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead);

(Str is a TFileStream )

I suppose the message speaks for itself, but I absolutely must avoid it. This exception occurs only in debug mode, but I need to debug this thing without worrying about getting this message all the time.

Oddly enough, most of the time the file is downloaded and sent back anyway.

How could I avoid this? And why does this not allow me to open it more than once, even if it is read only?

+4
source share
1 answer

As pointed out in a @ain comment, you skip sharing mode in your constructor.

Change it

 Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead); 

to that

 Str:= TFileStream.Create('C:\SomePath\SomeFile.html', fmOpenRead or fmShareDenyNone); 
+6
source

Source: https://habr.com/ru/post/1403247/


All Articles