[英]@Url.Content with external js file
我正在使用blueimp upload
庫。 我在其他javascript
文件中上傳了代碼。 我正在使用ASP.Net MVC
。 我想通過Url.Content("~")
或其他方式(如果有)來獲取我的應用程序的根目錄。 我嘗試過此解決方法,但是結果是給我字面量'@ Url.Content(“〜”)'。 所以我認為使用html的data-*
。 現在我的文件上傳看起來像
<input type="file" name="files[]" data-root ='<%= @Url.Content("~/") %>' multiple/>
並且上傳代碼是
$('input:file', subscriber).fileupload({
dataType: 'json',
url: $(this).data('root')+'ControllerName/ActionName',
done: function (e, data) {
if (!data || !data.result || !data.result.length) {
alert("Error occured when uploading the file");
return;
}
if (data.result[0].error) {
alert("Error occured. " + data.result[0].error);
return;
}
LoadHtmlInDiv('AssigneeDocList', '/Assignee/DocumentList');
},
fail: function (e, data) {
alert("Error occured when uploading the file");
}
});
但是它給出Error 404
因為新的URL就像
/ProjectName/ControllerName/undefinedControllerName/ActionName
我試圖在fail
回調中檢查相同值,這是正確的。 不知道為什么使用url會出錯。
我假設您的<input/>
標記在表單內。 如果是這樣,您可以將空字符串作為url
傳遞。 blueimp upload
上傳使用form
的action
方法url作為基礎,並將其自己的url部分附加到其中。 那就是為什么你得到這樣的網址
/ProjectName/ControllerName/undefinedControllerName/ActionName
在我自己的代碼中,我聲明了如下形式:
@using (Html.BeginForm("Upload", "Document", FormMethod.Post, new { enctype = "multipart/form-data" }))
我的input
標簽在里面,就像-
<input id="fileupload" type="file" name="file" />
和javascript-
$('#fileupload').fileupload({
url: '',
dataType: 'json',
add: function (e, data) {},
done: function (e, data) {},
progressall: function (e, data) {}
});
我已經刪除了函數實現,但是您可以理解。 將此文件發布到
/Document/Upload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.