Able to save JSON content as document and display download details at the bottom bar in Chrome.
using $window.open(url'_blank') for other browsers , so it opens as new tab/window.
Any help at the earliest would be great.
代码适用于chrome: 找到以下链接来保存jquery函数的文档样本
$.ajax({
async: false,
type: 'POST',
url: '../Document/DownloadDocument?DocumentID=123456',
data:$("#generateDocument").serialize),
success: function (DocFilePath) {
var link = document.createElement('a');
link.download = Filename + "_profile" + type;
link.onclick = downloadDoc(DocFilePath) ;
window.location.href = redirecturl;
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Error" + thrownError);
}
});
function downloadDoc(url) {
window.location = url;
}
答案 0 :(得分:0)
在控制器中创建两个动作结果一个用于处理,另一个用于下载文件。
public ActionResult DocumentProcess(string documentId)
{
//do your stuff what ever processing is..
//Create a filePath which you have to download.
string filePath = @"F:\umair\Documents\BioVeriSys-Interface Document.pdf";
if (!string.IsNullOrEmpty(documentId) ) //proccessing success then return with success and full filePath.
return Json(new { success = 1, message = "Document processing Successfully Completed", filePath = filePath }, JsonRequestBehavior.AllowGet);
else
return Json(new { success = 0, message = "documentId is required but its empy or null."}, JsonRequestBehavior.AllowGet);
}
public ActionResult DownloadFile(string fileName)
{
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
if (file.Exists)
return File(file.FullName, "application/pdf", file.Name +".pdf");
else
return HttpNotFound();
}
并在查看
中为了测试目的,我创建了两个按钮,一个用于documentId,另一个用于没有documentId。
<button onclick="myfunction(123)"> With documentId </button>
<br />
<br />
<button onclick="myfunction('')"> Without doucemntId </button>
@section Scripts{
<script type="text/javascript">
function myfunction(documentId) {
$.post('@Url.Action("DocumentProcess","Home")?documentId=' + documentId,
{ /*$("#generateDocument").serialize)*/ },
function (data) {
if (data.success > 0) {// mean server return with success message.
window.location = '@Url.Action("DownloadFile", "Home")?fileName=' + data.filePath;
} else { // means some validation not true
alert(data.message);
}
}
).fail(function (xhr, textStatus, errorThrown) {
alert(textStatus + ':' + errorThrown);
});
}
</script>
}