您好我正在嘗試下載mvc4中的文件。以下是我的代碼來下載文件。我正在做一個Ajax請求來獲取文件。我的控制器代碼看起來很好。變量結果將得到所需的字節。我在jquery的Download函數中遇到了成功函數的問題。控制不會成功的功能。即使我的警報不起作用。我是否缺少上述代碼中的任何內容?有人可以建議我嗎?無法在mvc4和jquery中返回文件
public ActionResult Download(int? ClientId)
{
service.Service objService = new service.Service();
DashboardBAL objdb = new DashboardBAL();
string filepath = objdb.GetFilepath(ClientId.GetValueOrDefault(0));
string folderName = @"F:\UploadedFile";
string serverMappath = (folderName);
byte[] result = objService.DownloadFileFromDMS(filepath);
string contentType = MimeMapping.GetMimeMapping(filepath);
string FileName = Path.GetFileName(filepath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = Path.GetFileName(filepath),
Inline = true,
};
if (!System.IO.Directory.Exists(serverMappath))
{
System.IO.Directory.CreateDirectory(serverMappath);
}
string strdocPath = serverMappath + @"\" + FileName;
if (result != null)
{
FileStream objfilestream = new FileStream(strdocPath, FileMode.Create, FileAccess.Write);
objfilestream.Write(result, 0, result.Length);
objfilestream.Close();
}
return File(result, System.Net.Mime.MediaTypeNames.Application.Octet, FileName);
}
function Download(pClientid) {
$.ajax(
{
type: "GET",
cache: false,
data: { ClientId: pClientid },
contentType: 'application/json; charset=utf-8',
dataType: "json",
cache: false,
url: '@Url.Action("Download", "TestRendering")',
// url: '/TestRendering/Download',
headers: {
'VerificationToken': forgeryId
},
success: function (data) {
alert(1);
ShowFiepopup(data);
}
});
}
function ShowFiepopup(FileName) {
$("#dialog").dialog({
modal: true,
title: "Preview of " + FileName,
width: 850,
height: 600,
buttons: {
Close: function() {
$(this).dialog('close');
}
},
open: function() {
var object = "<object data=\"{FileName}\" type=\"application/pdf\" Zoom=\"100%\" width=\"800px\" height=\"600px\">";
object += "If you are unable to view file, you can download from <a href=\"{FileName}\">here</a>";
object += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
object += "</object>";
object = object.replace(/{FileName}/g, "../UploadedFile/" + FileName);
$("#dialog").html(object);
}
});
};
function Download(pClientid) {
var fileUrl = '@Url.Action("Download", "TestRendering",new {ClientID= "pClientid" })';
alert(fileUrl);
fileUrl = fileUrl.replace("_X_", fileName);
ShowFiepopup(fileUrl)
}
是什麼問題..是否會引發任何錯誤的操作方法? –
不,它沒有返回任何東西。我有Jquery功能,下載。在成功塊我沒有得到任何東西。即使我提出了一個警報,但它也不起作用 –
您是否在調試器控制檯中查看了網絡選項卡?您回覆的回覆是什麼?\ – arjabbar