[英]Download selected files in knockout js
I need to download the selected files in a grid on download button click. 单击下载按钮时,我需要将所选文件下载到网格中。 I am using knokout.js and web api Using the below given response I was able to download a single file 我正在使用knokout.js和Web API使用下面给出的响应,我能够下载一个文件
self.DownloadDoc = function () {
window.location.assign("http://localhost:8092/api/Document/GetResponse?id=" + self.selectedDocuments()[0]);
self.selectedDocuments.removeAll();
self.bindDocuments();
};
I tried looping this window.location.assign() code using for loop,but as it is synchronous call,its downloading only one file. 我尝试使用for循环循环运行window.location.assign()代码,但由于它是同步调用,因此仅下载一个文件。
Html part HTML部分
<a href="#" data-bind="click :$root.DownloadDoc,visible: $root.DownloadDocVisible"><span>Download</span><small></small></a>
Web Api code Web API代码
[HttpGet]
public HttpResponseMessage GetResponse(string id)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
List<DocumentsModel> documents = _service.GetDocumentContent(Convert.ToInt32(id));
byte[] fileBytes = documents.FirstOrDefault().FileContent;
System.Net.Http.Headers.MediaTypeHeaderValue mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
if (fileBytes != null)
response.Content = new ByteArrayContent(fileBytes);
response.Content.Headers.ContentType = mediaType;
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = documents.FirstOrDefault().FileLeafRef;
return response;
}
this part self.selectedDocuments()[0]
, looks like it will force download always only of first document. 这部分self.selectedDocuments()[0]
,看起来它将强制仅总是下载第一个文档。 Not sure what is your structure and output of selectedDocuments(), for example, if it outputs array, with fileID nodes, maybe you should try with this: 不知道您的selectedDocuments()的结构和输出是什么,例如,如果它输出带有fileID节点的数组,也许您应该尝试以下操作:
<div data-bind="foreach: self.selectedDocuments()">
<a href="#" data-bind="click :$root.DownloadDoc(fileID),visible: $root.DownloadDocVisible"><span>Download</span><small></small></a>
</div>
then, you will have to pass param in function: 然后,您将必须在函数中传递参数:
self.DownloadDoc = function (fileID) {
window.location.assign("http://localhost:8092/api/Document/GetResponse?id=" + fileID);
self.selectedDocuments.removeAll();
self.bindDocuments();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.