简体   繁体   English

在浏览器中下载JavaScript提示

[英]JavaScript Prompt download in browser

Im simply trying to download a file from the server and prompt the download in the browser for the user to see. 我只是尝试从服务器下载文件,然后在浏览器中提示下载以供用户查看。

What i have right now: Client 我现在所拥有的:客户

export function downloadTemplateAction(questionnaire) {
  return dispatch => {
    dispatch(downloadTemplateRequestAction(questionnaire));

    return request
      .get(downloadGETUrl)
      .end((err, res) => {
        if (err) {
          console.log("Download ERROR", err)
          dispatch(downloadTemplateFailureAction(err, questionnaire));
        } else {
          console.log("Download Success", res.body)
          dispatch(downloadTemplateSuccessAction(res.body, questionnaire));
        }
      });
  }
}

Server: 服务器:

export function downloadTemplateDocument(req, res){
    res.download('template/Example.docx');
    res.end();
}

Im facing two problems: 我面临两个问题:

First : When trying to download the file via the function of the Client, the response body is null but success and nothing more happens. 第一 :尝试通过客户端功能下载文件时,响应主体为null,但成功,没有任何反应。

Second : When contacting the get API via the browser localhost:3002/api/download, the download works but the received file is empty. 第二 :通过浏览器localhost:3002 / api / download与get API联系时,下载有效,但收到的文件为空。 There should be text in it. 里面应该有文字。

What am i doing wrong here? 我在这里做错了什么?

Browser can't prompt a download progress because your request is sent via XMLHttpRequest. 浏览器无法提示下载进度,因为您的请求是通过XMLHttpRequest发送的。 Physical access to the file is needed for the browser to be aware of any download. 浏览器需要物理访问该文件,才能知道所有下载。

You could use download attribute to tell browser to download linked ressource. 您可以使用download属性来告诉浏览器下载链接的资源。

original answer 原始答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM