Returning binary file from controller in ASP.NET Web API -
I am working on a web service using the new webapp of ASP.NET MVC which will complete the binary files, Mostly The following control method works, which means that it gives a file, but this content type is Is there a better way to do this? Its a You should not stream Ensure that the current state of the stream is set to 0 (that is, the beginning of the data of the stream). In the above example, it has been given because you have just opened the file. However, in other scenarios (such as when you first write some binary data in Obviously permission can help prevent access rights issues on the web server, with specified file streams; IIS application pool accounts are often granted read / list / executable access rights to wwwroot only. .cab and
.exe files.
Application / Jason :
Public HTTPPRESS Pacemase & lt; Stream & gt; Post (string version, string environment, string file type) {var path = "" C: \ temp \ test.exe "; Var stream = new filestream (path, filemod.open); new HTTPPRESS pacesmasters & lt; stream & gt; Return the stream, the new MediaTapHeader value ("App / octet-stream");}
content to use one with the property Shish:
// Using System.IO; // Using System.Net.Http; // Using System.Net.Http.Headers; Public HttpResponseMessage Post (string Version, string environment, string filetype) {var path = "" C: \ temp \ test.exe "; HttpResponseMessage results = new HTTPPasspacemessage (HTTPPatus code.key); Var Stream = New File Stream (Path, Filemod. Open, FileAccess. Read); result. Content = new stream contact (stream); Results: music. Header. Content type = new media tag header value ("app / octet-stream"); Return result; } Some things to keep in mind about
stream :
Since the web API still needs to be able to access it, when it processes the
result of the controller method to return the data to the customer, then,
Do not use (var stream = รข ?? |) | The Block Web API will settle the stream for you.
MemoryStream ), make sure that
stream.Seek (0, SeekOrigin.Begin); or set
stream.Position = 0;
Comments
Post a Comment