Link does not open after streaming document data down

I think I am missing JavaScript code. I upload documents for each request. When the user clicks on the link, I get the document data and transmit it. I see on Fiddler that the data is being omitted, but the .txt document link does not open.

[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; } 

First, I downloaded all the documents for this request, and if the user clicks on the file, I invoke the action of the download stream.

  $.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. Somehow it loads data, but the link does not open. Could there be a content type not set or that it considers JSON data? Help with some ideas, please.

Here's a 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) { } }); } 
+5
source share
1 answer

You cannot easily upload a file through an Ajax request. I recommend posting data on a blank page from a form, instead of Ajax (you can fill out this form and post it through jQuery if you need to). If this interests you, I will guide you through it, just let me know.

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

0
source

All Articles