久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='cjVZY'></bdo><ul id='cjVZY'></ul>
      <legend id='cjVZY'><style id='cjVZY'><dir id='cjVZY'><q id='cjVZY'></q></dir></style></legend><tfoot id='cjVZY'></tfoot>
      <i id='cjVZY'><tr id='cjVZY'><dt id='cjVZY'><q id='cjVZY'><span id='cjVZY'><b id='cjVZY'><form id='cjVZY'><ins id='cjVZY'></ins><ul id='cjVZY'></ul><sub id='cjVZY'></sub></form><legend id='cjVZY'></legend><bdo id='cjVZY'><pre id='cjVZY'><center id='cjVZY'></center></pre></bdo></b><th id='cjVZY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='cjVZY'><tfoot id='cjVZY'></tfoot><dl id='cjVZY'><fieldset id='cjVZY'></fieldset></dl></div>

      <small id='cjVZY'></small><noframes id='cjVZY'>

        使用 XMLHttpRequest 提示文件下載

        Prompt file download with XMLHttpRequest(使用 XMLHttpRequest 提示文件下載)
        <legend id='4lOdt'><style id='4lOdt'><dir id='4lOdt'><q id='4lOdt'></q></dir></style></legend>

        <tfoot id='4lOdt'></tfoot>
                • <bdo id='4lOdt'></bdo><ul id='4lOdt'></ul>

                  <small id='4lOdt'></small><noframes id='4lOdt'>

                  <i id='4lOdt'><tr id='4lOdt'><dt id='4lOdt'><q id='4lOdt'><span id='4lOdt'><b id='4lOdt'><form id='4lOdt'><ins id='4lOdt'></ins><ul id='4lOdt'></ul><sub id='4lOdt'></sub></form><legend id='4lOdt'></legend><bdo id='4lOdt'><pre id='4lOdt'><center id='4lOdt'></center></pre></bdo></b><th id='4lOdt'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4lOdt'><tfoot id='4lOdt'></tfoot><dl id='4lOdt'><fieldset id='4lOdt'></fieldset></dl></div>
                    <tbody id='4lOdt'></tbody>
                  本文介紹了使用 XMLHttpRequest 提示文件下載的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我知道 jQuery 的 ajax 方法無法處理下載,我不想添加 jQuery 插件來執行此操作.

                  I'm aware that jQuery's ajax method cannot handle downloads, and I do not want to add a jQuery plugin to do this.

                  我想知道如何使用 XMLHttpRequest 發送 POST 數據來下載文件.

                  I want to know how to send POST data with XMLHttpRequest to download a file.

                  這是我嘗試過的:

                  var postData = new FormData();
                  postData.append('cells', JSON.stringify(output));
                  
                  var xhr = new XMLHttpRequest();
                  xhr.open('POST', '/export/', true);
                  xhr.setRequestHeader("X-CSRFToken", csrftoken);
                  xhr.responseType = 'arraybuffer';
                  xhr.onload = function (e) {
                      console.log(e);
                      console.log(xhr);
                  }
                  xhr.send(postData);
                  

                  我正在使用 Django,并且文件似乎已成功發送回客戶端.在 Chrome 的網絡選項卡中,我可以在預覽選項卡中看到亂碼(這是我所期望的).但我想發回一個 zip 文件,而不是 zip 文件的文本表示.這是 Django 后端:

                  I'm working with Django, and the file appears to be sending back to the client successfully. In the network tab in Chrome, I can see gibberish in the preview tab (which I expect). But I want to send back a zip file, not a text representation of the zip file. Here's the Django back end:

                  wrapper = FileWrapper(tmp_file)
                  response = HttpResponse(wrapper, content_type='application/zip')
                  response['Content-Disposition'] = "attachment; filename=export.zip"
                  response['Content-Length'] = tmp_file.tell()
                  return response
                  

                  我已經搜索了好幾個小時,但沒有找到關于如何使用 XMLHttpRequests 執行此操作的適當示例.我不想使用 POST 操作創建正確的 html 表單,因為表單數據相當大,并且是動態創建的.

                  I've searched this for hours now without finding a proper example on how to do this with XMLHttpRequests. I don't want to create a proper html form with a POST action because the form data is rather large, and dynamically created.

                  上面的代碼有問題嗎?我錯過了什么?我只是不知道如何實際將數據作為下載發送到客戶端.

                  Is there something wrong with the above code? Something I'm missing? I just don't know how to actually send the data to the client as a download.

                  推薦答案

                  UPDATE:自從引入 Blob API.詳情請參考 Steven 的回答.

                  UPDATE: this answer is not accurate anymore since the introduction of Blob API. Please refer to Steven's answer for details.

                  原始答案:

                  XHR 請求不會觸發文件下載.我找不到明確的要求,但 W3C doc on XMLHttpRequest 沒有描述任何特殊對 content-disposition=attachment 響應的反應

                  XHR request will not trigger file download. I can't find explicit requirement, but W3C doc on XMLHttpRequest doesn't describe any special reaction on content-disposition=attachment responses either

                  如果不是 POST 請求,您可以通過 window.open() 在單獨的選項卡中下載文件.這里建議使用帶有 target=_blank

                  You could download file by window.open() in separate tab, if it was not POST request. Here it was suggested to use a hidden form with target=_blank

                  這篇關于使用 XMLHttpRequest 提示文件下載的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在調用 abort (jQuery) 之后,瀏覽器也會等待 ajax 調用完成)
                  JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不適用于 IE?)
                  XMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 無法加載,請求的資源上不存在“Access-Control-Allow-Origin標頭) - IT屋-程序員軟件開發技術分
                  Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 請求是否有可能不遵循重定向 (301 302))
                  XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分內容)
                  Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)
                • <tfoot id='wKeXH'></tfoot>
                • <small id='wKeXH'></small><noframes id='wKeXH'>

                  <i id='wKeXH'><tr id='wKeXH'><dt id='wKeXH'><q id='wKeXH'><span id='wKeXH'><b id='wKeXH'><form id='wKeXH'><ins id='wKeXH'></ins><ul id='wKeXH'></ul><sub id='wKeXH'></sub></form><legend id='wKeXH'></legend><bdo id='wKeXH'><pre id='wKeXH'><center id='wKeXH'></center></pre></bdo></b><th id='wKeXH'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='wKeXH'><tfoot id='wKeXH'></tfoot><dl id='wKeXH'><fieldset id='wKeXH'></fieldset></dl></div>
                    <tbody id='wKeXH'></tbody>

                        • <legend id='wKeXH'><style id='wKeXH'><dir id='wKeXH'><q id='wKeXH'></q></dir></style></legend>
                            <bdo id='wKeXH'></bdo><ul id='wKeXH'></ul>
                            主站蜘蛛池模板: 成人久久久 | 久久久免费在线观看 | 日韩精品视频中文字幕 | 最近中文字幕第一页 | 国产欧美性成人精品午夜 | 91九色网站 | 999精品视频 | 欧美精品一二三区 | 欧美激情精品久久久久久变态 | 久国产精品| 日本黄色一级片视频 | 精品久久久久久国产 | 精品一区二区三区中文字幕 | 中文字幕一区二区三区精彩视频 | 亚洲欧美日韩在线 | 欧美视频精品 | 福利在线看 | 国产免费又色又爽又黄在线观看 | 婷婷福利视频导航 | 精品视频一区二区 | 日韩在线免费 | 在线a视频 | 久久精品国产99国产精品 | 欧美成人精品激情在线观看 | 中文区中文字幕免费看 | 99精品国产一区二区三区 | 日中文字幕在线 | 免费日本视频 | 一区二区三区在线免费观看视频 | 免费看一区二区三区 | 韩国理论电影在线 | 男人午夜视频 | gav成人免费播放视频 | 久久这里只有精品首页 | 亚洲一区二区在线电影 | 日韩毛片 | 日日夜夜草 | 国产欧美精品在线观看 | 一区视频在线免费观看 | 国产精品久久在线观看 | 99久久精品免费看国产四区 |