C # file upload is corrupt

I have C # in a utility for a web API project. The code loading part is working fine; I checked the file that gets to the server matches the downloaded file. However, something happens in the download that causes the client to view the file as damaged, and when I do the diff, I see that something went wrong.

Code Compare diff from files

Unfortunately, I cannot understand what I am doing wrong. The relevant parts of the utility are as follows:

public static HttpResponseMessage StreamResponse(this HttpRequestMessage request, Stream stream) { if (stream.CanSeek) stream.Position = 0;// Reset stream if possible HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK); response.Content = new StreamContent(stream); if (stream is FileStream) {// If this is a FileStream, might as well figure out the content type string mimeType = MimeMapping.GetMimeMapping(((FileStream)stream).Name); response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType); } return response; } public static HttpResponseMessage DownloadAs(this HttpResponseMessage response, string fileName) { response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); response.Content.Headers.ContentDisposition.FileName = fileName; response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(fileName)); return response;// For chaining or whatnot } 

My use in API controllers return ResponseMessage(Request.StreamResponse(stream).DownloadAs("Filename.ext"));

I have double-checked code to download, and it looks like what I found. What am I doing wrong or what am I missing? There seems to be something wrong with the encoding or encoding, but I cannot say what the solution is.

+3
c # asp.net-web-api download
source share
1 answer

Finally figured out the problem thanks to this Q&A . I was missing the parameter / parameter responseType in $ http to call client-side code.

0
source share

All Articles