使用angualr和前preSS JS下载所有的文件格式 [英] download all file formats using angualr and express JS

查看:120
本文介绍了使用angualr和前preSS JS下载所有的文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下载的角度JS所有文件格式。与code pieace我,我可以下载的文本格式。但是,对于其他所有的格式,我不能够打开下载的文件。我的REST服务提供响应的内容类型:应用程序/八位字节流。我的codeBase的调用前press.js与内容型res.send(体)的方法:应用程序/ JSON。下载的图像尺寸也增加了一倍,我可以看到身体的类型为字符串。

我试图调用res.download(路径,文件名,回调)前press.js与内容类型的功能:应用程序/八位字节流,如果请求下载通话。它是一个正确的做法?另外我不知道在res.download函数发送的路径,作为文件来自服务器结束。请帮我在这。

我的客户code:

  $ scope.downloadFile =功能(键){
  service.downloadFile()。然后(功能(结果){
    VAR BLOB =新的Blob([结果] {
      类型:应用程序/八位字节流;字符集= UTF-8
    });
    VAR urlCreator = window.URL || window.webkitUR​​L || window.mozURL || window.msURL;
    VAR URL = urlCreator.createObjectURL(BLOB);
    VAR元= angular.element('< A />');
    element.attr({
      HREF:URL,
      目标:'_blank',
      下载:关键
    })[0]。点击();
  });
}


解决方案

我的一个同事共享从而解决了我的问题的链接。还有就是输入流直接的方式来代理。这个细节可在 https://github.com/request/request

  req.pipe(请求(网址))。管(RESP)

通过这个流可以提供给客户端和客户端可以调用链接/ URL来利用附加的文件。

客户端code:

  $ scope.downloadFile =功能(键){
   VAR URL =的http://本地主机:8080 /的GetFile';
    VAR元= angular.element('< A />');
    element.attr({
      HREF:URL,
      目标:'_blank',
      下载:文件名
    })[0]。点击();
  });
}

I'm working on download all file formats in angular js. with the code pieace i've i can download text formats. But for all other formats, i'm not able to open the downloaded files. My REST service gives response content-type: application/octet-stream. my codebase calls res.send(body) method of express.js with content-type:application/json. The downloaded image size is doubled also i could see the type of the body is string.

I'm trying to invoke res.download(path, filename, callback) function of express.js with content-type:application/octet-stream if the request is download call. Is it a correct approach? Also i'm not sure which path to send in res.download function, as the file comes from server end. Kindly help me on this.

my client code:

$scope.downloadFile = function(key) {
  service.downloadFile().then(function(result) {
    var blob = new Blob([result], {
      type: 'application/octet-stream; charset=utf-8'
    });
    var urlCreator = window.URL || window.webkitURL || window.mozURL || window.msURL;
    var url = urlCreator.createObjectURL(blob);
    var element = angular.element('<a/>');
    element.attr({
      href: url,
      target: '_blank',
      download: key
    })[0].click();
  });
}

解决方案

One of my colleague shared a link which resolved my problem. There is way to proxy the incoming stream directly. This detail is available in https://github.com/request/request

req.pipe(request('url')).pipe(resp)

With this the stream can be made available to client and client can invoke the link/url to avail the attached file.

Client code:

$scope.downloadFile = function(key) {
   var url = 'http://localhost:8080/getFile';
    var element = angular.element('<a/>');
    element.attr({
      href: url,
      target: '_blank',
      download: fileName
    })[0].click();
  });
}

这篇关于使用angualr和前preSS JS下载所有的文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆