流式传输文档数据后链接未打开

Link not opening after streaming data of the document down

本文关键字:链接 数据 传输 文档      更新时间:2023-09-26

我想我在JavaScript方面缺少一些代码。我正在下载每个请求的文件。当用户单击链接时,我将获取文档数据并将其流式传输。我在Fiddler上看到数据正在下来,但是。txt文档链接没有打开。

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

首先,我下载了该请求的所有文档,如果用户单击该文件,我调用下载流操作。

  $.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) {
                     }
                 });

我想我在成功之后错过了一些东西。它下载了数据,但是链接没有打开。可能是内容类型没有设置,或者它认为这是JSON数据?请帮忙出主意。

链接:

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) {
                }
            });              
        }

无法通过Ajax请求轻松下载文件。我建议将数据从表单发送到空白页面,而不是Ajax(如果需要,您可以填充该表单并通过jQuery发送)。如果你感兴趣,我可以指导你,请告诉我。

如果你仍然想从Ajax下载,我建议你参考这篇文章。