Uploading Excel file via web api. Damage

I am trying to upload an excel file via Web API (using Entity framework). The download works, but when I try to open the file, I get a file error about file corruption.

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 a file with two error dialogs and the following message.

Excel finished checking and repairing file level. Parts of this book may have been repaired or discarded

enter image description here

enter image description here

+3


source to share





All Articles