5

I think I am missing some code on the JavaScript side. I am downloading the documents for each request. When the user clicks on the link, I go get the document data and stream it down. I see on Fiddler that the data is coming down, but the .txt document link is not opening.

[HttpGet]
    public HttpResponseMessage GetDataFiles(Int64 Id)
    {
        var results = context.PT_MVC_RequestFile.Where(x => x.RowId == Id).FirstOrDefault();
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

        try
        {
            if (results != null)
            {
                response.Headers.AcceptRanges.Add("bytes");
                response.StatusCode = HttpStatusCode.OK;
                response.Content = new ByteArrayContent(results.Data);
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = results.FileName;
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentLength = results.Data.Length;
            }
        }
        catch (EntityException ex)
        {
            throw new EntityException("GetFiles Failed" + ex.Message);
        }

        return response;
    }

Firstly, I downloaded all the documents for that request, and if the user clicks on the file, I call the download stream action.

  $.ajax({
                     url: url,
                     type: 'GET',
                   //  data: JSON.stringify(model, null),
                     contentType: "application/json",
                     success: function (data) {

                         if (data != "") {
                             $("#fileLength").val(data.length);
                            // alert(data.length);
                            $.each(data, function (i, item) {
                                 var newDiv = $(document.createElement('div')).attr("id", 'file' + i);
                                 newDiv.html("<input id=\"cb" + i + "\" type=\"checkbox\"> &nbsp; <a href=\"#\" onclick=\"GetData('" + item.RowId + "','" + item.mineType + "')\"   >" + item.FileName + "</a>");
                                 newDiv.appendTo("#fileRows");
                             });
                         } else {

                        }
                     },
                     error: function (xhr, ajaxOptions, thrownError) {

                     }
                 });

I think I am missing something after success though. Somehow it downloads the data, but the link does not open. Could it be the content type is not set, or that it thinks it is JSON data? Help with some ideas please.

Here is the link:

function GetData(rowId,mineType) {
           // alert(mineType);
            var url = "api/MyItemsApi/GetDataFiles/" + rowId;

            $.ajax({
                url: url,
                type: 'GET',
                //data: JSON.stringify(model, null),
                contentType: "application/json",
                success: function (data) {

                },
                error: function (xhr, ajaxOptions, thrownError) {

                }

            });              
        }
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
georges
  • 51
  • 2
  • HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Length: 6429 Content-Type: application/txt Expires: -1 Accept-Ranges: bytes Server: Microsoft-IIS/8.0 Content-Disposition: attachment; filename=Bare.txt X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcYzBhYmRnZVxEb2N1bWVudHNcVmlzdWFsIFN0dWRpbyAyMDEyXFByb2plY3RzXFByb2plY3QuVHJhY2tlci5XZWIuU29sdXRpb25cUHJvamVjdC5UcmFja2VyLldlYlxhcGlcTXlJdGVtc0FwaVxHZXREYXRhRmlsZXNcMQ==?= X-Powered-By: ASP.NET Date: Thu, 30 Jul 2015 13:33:46 GMT here is what coming down in the header,I changed it to txt but still no luck – georges Jul 30 '15 at 13:42

1 Answers1

0

You can't easily download a file through an Ajax request. I recommend to post the data to a blank page from a form, instead of the Ajax (you can populate that form and post it via jQuery if you need to). If you're interested, I could guide you through it, just let me know.

If you still want to download from Ajax, I suggest you refer to this post.

Community
  • 1
  • 1
Javier
  • 21
  • 3