【问题标题】:async file upload does not prompt browser to download file异步文件上传不提示浏览器下载文件
【发布时间】:2014-06-12 12:59:27
【问题描述】:

我创建了一个不提示用户下载文件的异步文件下载方法。没有错误,代码和jquery运行良好,但是浏览器没有提示下载。我已经查看了关于 SO 的所有解决方案,但我的似乎不起作用。

public async Task<HttpResponseMessage> Method(string data)
{
    //do stuff

     response.StatusCode = HttpStatusCode.OK;
     response.Content = new StreamContent(new FileStream(downloadFileName, FileMode.Open, FileAccess.Read));
     response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");                        
     response.Content.Headers.ContentDisposition.FileName = myFileName;
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

      return response;

}

【问题讨论】:

  • 这段代码{FileName = myFileName};在你的其他response字段设置代码中间做什么?
  • 没有意识到我设置了 FileName 两次。在编辑中将其删除,但这与我的问题无关。
  • 在页面上放置 iframe 解决了问题,但必须有另一种方法

标签: jquery ajax file-upload asp.net-web-api


【解决方案1】:

在 $.get 完成后添加 iframe 并更改其源代码让浏览器打开下载对话框。

function callback(result) {

                $('#myFrame').attr('src', '/worksitefileservice/api/xfer/download?data=' + encodeURIComponent(json));
            }

<iframe id="myFrame" style="visibility:hidden;display:none;" src=""></iframe>

【讨论】:

    猜你喜欢
    • 2012-07-04
    • 2012-01-18
    • 1970-01-01
    • 2010-10-18
    • 2018-11-05
    • 2011-03-03
    • 2013-10-15
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多