Martea
Martea

Reputation: 487

WebAPI returning File and download is not starting

Having a very strange problem, when I'am returning a file as a "HttpResponseMessage", it works when iam returning pdf file, but when I'am returning a xls file(excel file) it does not.

Here is the response headers, I've tried changing the content type to "application/vnd.ms-excel"

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/octet-stream
Content-Encoding: UTF-8
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=f.xls
Content-Description: File transfer
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 07:49:47 GMT

the result iam getting looks like when you open the xls file in notepad.

i foudn a very strange solution that was to convert it to base64 and make a link to "data: application/octet-stream; base64, " and that would start the download thou you dont know the name of the file so the user would not understand to save it as XLS file...

now iam back at the result that wont start the download... any suggestions?


Here comes the response Raw from fiddler2

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/8.0
Set-Cookie: 
path=/; HttpOnly
Content-Disposition: attachment; filename=f.xls
Content-Description: File transfer
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 08:40:51 GMT

��ࡱ�����������������>����   ���������������d�������������������������e��f��g��h��i������������������������������������

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***

UPDATE

Iam calling the WEBAPI function with a post request, and i compare it with the old response result and they are almost the same.

iam posting the respnose from both requests here.

from the old method, the download starts...

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/vnd.ms-excel; charset=utf-8
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=CallList_2013-12-06.xls
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcZUNSTVxDYWxsTGlzdEV4cG9ydC5hc3B4?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 10:11:13 GMT
Content-Length: 34590

from the new method, download dosent start.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/vnd.ms-excel; charset=utf-8
Content-Encoding: UTF-8
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=CallList_2013-12-06.xls; charset=utf-8
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 11:04:49 GMT

UPDATE added Method where i return the result the ByteResult class only contains byte[], contentLength, contentType and filename

 protected HttpResponseMessage ByteResult(ByteResult result)
        {
            try
            {


                var responseStream = new MemoryStream();
                var buffer = result.Data;
                responseStream.Write(buffer, 0, Convert.ToInt32(result.ContentLenght));

                // No Range header. Return the complete file.
                responseStream.Position = 0;

                var response = new HttpResponseMessage
                {
                    StatusCode = HttpStatusCode.OK,
                    Content = new StreamContent(responseStream)
                };


                response.Content.Headers.Add("Content-Disposition", "attchment; filename=" + HttpUtility.UrlEncode(result.Name, Encoding.UTF8));
                response.Content.Headers.Add("Content-Type", result.ContentType );
                response.Content.Headers.Add("Content-Description", "File Download");
                response.Content.Headers.Add("Content-Encoding", "UTF-8");
                response.Content.Headers.Add("Charset", "UTF-8");
                response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now);


                return response;
            }
            catch (IOException ioex)
            {
                return ErrorResult(ioex, "Unknown IO Error", HttpStatusCode.InternalServerError);
            }
            catch (Exception ex)
            {

                return ErrorResult(ex, "Unknown Error", HttpStatusCode.InternalServerError);
            }


        }

Upvotes: 4

Views: 3808

Answers (1)

user1401260
user1401260

Reputation: 101

I'd try double checking the MIME Type mappings on the client that the excel file is being downloaded to.

Also, if you change the content type in the response to application/octet-stream the client should just ask if you if you want to save the file - if the saved file is then corrupt look further into the encoding of the file.

Also look in Fiddler to ensure that the response is terminated correctly, I've previously seen files sent correctly, but then other messages tagged onto the end of the response stream due to server errors - in my case because a view didn't exist when using Monorail - so we ended up with an excel spreadsheet followed by 500 error text.

HTH,

Nick

Upvotes: 1

Related Questions