问题
I am trying to download a excel file through Web API (using Entity framework). The download is working but I am getting some error dialog about file corrupt when trying to open the file.
Web API code as below:
public HttpResponseMessage GetValue(int ID, string name)
{
MemoryStream stream;
try {
using (DataContext db = new DataContext()) {
dynamic fileObj = (from c in db.FileList c.ID == IDc).ToList();
stream = new MemoryStream(fileObj(0).File);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue(fileObj(0).FileContentType);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = name };
return result;
}
} catch (Exception ex) {
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
It opens the file with two error dialog and following message.
Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded
来源:https://stackoverflow.com/questions/30925334/excel-file-download-through-web-api-getting-corrupt