在淘汰赛中下载所选文件

时间:2016-11-15 05:49:45

标签: javascript knockout.js knockout-3.0

我需要在点击下载按钮的网格中下载所选文件。我正在使用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();
}; 

我尝试使用for循环循环此window.location.assign()代码,但因为它是同步调用,所以它只下载一个文件。

Html部分

<a href="#" data-bind="click :$root.DownloadDoc,visible: $root.DownloadDocVisible"><span>Download</span><small></small></a>

网络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;
    }

1 个答案:

答案 0 :(得分:0)

这部分self.selectedDocuments()[0],看起来它会强制下载始终只有第一个文档。 不确定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>

然后,你必须在函数中传递param:

self.DownloadDoc = function (fileID) {
    window.location.assign("http://localhost:8092/api/Document/GetResponse?id=" + fileID);
    self.selectedDocuments.removeAll();
    self.bindDocuments();
};