开发者

How to save binary file on client in jquery .post

I have handler with this code:

    HttpRequest request = context.Request;
    HttpResponse response = context.Response;

    if (request["Type"] != null)
    {
        try
        {
            string resultFile = null;
            string fileName = string.Empty;

            int type = Convert.ToInt32(request["Type"]);

            switch (type)
            {
                case 1:
         开发者_StackOverflow           fileName = "InnerQuery.doc";
                    resultFile = GenerateInnerQuery(Id);
                    break;

                case 2:
                    fileName = "CourierQuery.doc";
                    resultFile = GenerateCourierQuery(Id);
                    break;

                case 3:
                    fileName = "TransportDogovor.doc";
                    resultFile = GenerateTransportDogovor(Id);
                    break;

                case 4:
                    fileName = "TransportQuery.doc";
                    resultFile = GenerateTransportQuery(Id);
                    break;

                case 5:
                    fileName = "PassQuery.doc";
                    resultFile = GeneratePassQuery(Id);
                    break;
            }

            if (resultFile != null)
            {
                response.Clear();
                response.AddHeader("pragma", "no-cache");
                response.AddHeader("cache-control", "private");
                response.CacheControl = "no-cache";
                response.ContentType = "application/octet-stream";
                response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}",
                    System.Web.HttpUtility.UrlPathEncode(fileName)));
                response.OutputStream.Write(File.ReadAllBytes(resultFile), 0, (int)(new FileInfo(resultFile)).Length);
                response.End();
            }
        }
        catch (Exception ex)
        { }
    }

On client i post data to handler with jQuery.post():

var handler = "GetWord.ashx?Type=6";
    var initiationDateFrom = $("#<%=InitiationDateFromTxt.ClientID%>").val();
    var initiationDateTill = $("#<%=InitiationDateTillTxt.ClientID%>").val();
    var queryDateFrom = $("#<%=QueryDateFromTxt.ClientID%>").val();
    var queryDateTill = $("#<%=QueryDateTillTxt.ClientID%>").val();
    var queryNumber = $("#<%=NumberTxt.ClientID%>").val();
    var initiator = $("#<%=InitiatorTxt.ClientID%>").val();
    var state = $("#<%=StateList.ClientID%>").val();
    $.post(handler,
    {
        InitiationDateFrom: initiationDateFrom,
        InitiationDateTill: initiationDateTill,
        QueryDateFrom: queryDateFrom,
        QueryDateTill: queryDateTill,
        QueryNumber: queryNumber,
        Initiator: initiator,
        State: state
    }, function (data) {
       /*here i should save my file*/
    });

I recieve binary word-file in "data", but can't to save it on client.

last time i use:

window.location = handler;

but with jQuery.post it not work.


THIS QUESTION's ANSWERS NEEDS A BIT OF AN UPDATE I THINK

I have come across following library: https://github.com/eligrey/FileSaver.js

Also in my case it was as simple as following:

At least here is the solution that works for me and i only needed to support it in chrome (v37).

Preface:

I needed to download excel file via post request. So here is how I have tackled it after reading up on few specs and q&as.

In my ajax call set up, i set response type to 'blob':

'responseType': 'blob'

And once successful response is received you would need to do minor DOM trickery to trigger download as follows:

var downloadLink = document.createElement('a');
downloadLink.download = 'export.xlsx';
downloadLink.innerHTML = 'Download File';
downloadLink.href = window.webkitURL.createObjectURL(data);
downloadLink.click();


When you request the data with an AJAX request, you need to handle it in JavaScript. Saving a file will not be available.

Instead of using an AJAX request you need to create a form that holds your parameters. Direct the response into an hidden iframe using the target attribute and the browser will offer to save the file.


As mentioned above, there are a number of different approaches. You may want to look at jQuery File Upload first.

Secondarily, you may want to look at a couple of options that include flash in the solution ( uploadify or swfupload). I have used both in the past and they met my needs satisfactorily - but not perfectly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜