Excel File download through web api. Getting corru

2019-04-11 19:47发布

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

enter image description here

enter image description here

0条回答
登录 后发表回答