当用户单击调用Jquery Ajax函数的行时,我有一个使用JQuery手动创建的html表。此函数调用ASP.NET MVC控制器,该控制器调用在内部从文件服务器中提取文件,并将其存储在文件夹“ .. \ download \”下的Project Directory中。 我需要在浏览器中返回此文件JQuery和可下载文件。
调用API并将文件(任何类型)存储在项目目录中:
var response = _httpClient.GetAsync(documentURL).Result;
HttpContent content = response.Content;
string currfile = System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/download/"));
System.IO.DirectoryInfo di = new DirectoryInfo(currfile);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
using (var file = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/download/{0}.{1}", fileName, fileExt)), FileMode.CreateNew))
{ // create a new file to write to
Stream contentStream = content.ReadAsStreamAsync().Result; // get the actual content stream
contentStream.CopyToAsync(file); // copy that stream to the file stream
file.FlushAsync(); // flush back to disk before disposing
}
jQuery通话:
$("#DivAttachmentDetails tr:not(:first)").click(function () {
var DocumentumId = $(this).find("td:eq(0)").find(':input[name=hdnDocumentumId]').val();//$('#hdnDocumentumId').val();
$.ajax({
type: 'POST',
url: getExactPath('/Supplier/GetDocument'),
data: {
documetObj: DocumentumId
},
dataType: 'json',
async: false,
success: function (jsonData) {
},
error: function () {
alert("Unable to fetch the Document.");
}
});
});
如何使用JQuery将文件立即返回浏览器。
答案 0 :(得分:0)
将文件添加到“ .. \ download \ filename.pdf”后,我将发送回连接的foldername + filename。我存储在解决方案目录文件夹中,因为页面使用率少于5个用户。
string returnurl:
using (var file = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/download/{0}.{1}", fileName, fileExt)), FileMode.CreateNew))
{ // create a new file to write to
Stream contentStream = content.ReadAsStreamAsync().Result; // get the actual content stream
contentStream.CopyToAsync(file); // copy that stream to the file stream
file.FlushAsync(); // flush back to disk before disposing
returnurl = "\download\"+ fileName+"."+fileExt
}
jQuery:
success: function (jsonData)
{
window.open(jsonData.returnurl) // this opens the file in new windows as "http:\\projectname:8080\download\yourfilename.pdf" ( any file type)
},