[英]Unable to upload call post for transfering group of files to Action
問題是,當將File類型對象的數組傳遞給MVC模型操作時,$ .post調用存在問題
我正在做的是使用jquery.filedrop庫多次下載。 然后,在刪除所有項目並按下按鈕后,所有這些文件都將被發送到名為Upload的Action。
Html代碼
<h2>Upload</h2>
<legend>Upload a file</legend>
<div class="editor-field">
<input type="file" id="file" name="file" />
</div>
<h2>Drag & Drop file upload </h2>
<div id="dropArea"></div>
<h4>Uploaded files : </h4>
<ul class="list-group" id="uploadList"></ul>
<input id="submsion" type="submit" />
Javascript代碼:
$(function () {
$('#dropArea').filedrop({
allowedfileextensions: ['.doc', '.docx', '.pdf', '.jpg', '.jpeg', '.png', '.PNG', '.gif', '.txt'],
maxfiles: 3,
maxfilesize: 5, // in MB
dragOver: function () {
$('#dropArea').addClass('active-drop');
},
dragLeave: function () {
$('#dropArea').removeClass('active-drop');
},
drop: function () {
$('#dropArea').removeClass('active-drop');
},
afterAll: function (e) {
$('#dropArea').html('file(s) uploaded successfully');
},
uploadFinished: function (i, file, response, time) {
$('#uploadList').append('<li class="list-group-item">' + file.name + '</li>')
addToFilePile(file)
}
})
})
var arrayOfFiles = [];
var singularObject;
$('#file').on('change', function () {
singularObject = $('#file').val();
});
function addToFilePile(file) {
arrayOfFiles.push(file);
}
$('#submsion').click(function () {
var finalPile = [];
if (arrayOfFiles != null) {
finalPile = arrayOfFiles;
if (singularObject != null) {
finalPile.push(singularObject);
}
}
else if (singularObject != null) {
finalPile = singularObject;
}
else {
//both are empty - do something about it
}
console.log(finalPile);
$.post('@Url.Action("Home", "Upload")', { files: finalPile }, function (data) {
console.log("POST done");
});
});
C#動作代碼:
[HttpPost]
[ActionName("Upload")]
public ActionResult Upload(IEnumerable <HttpPostedFile> files)
{
if (files != null)
{
foreach (var item in files)
{
if (item != null)
{
string pathname = Path.Combine(@"D:\Projects\SavingFile\UploadedFiles", Path.GetFileName(item.FileName));
item.SaveAs(pathname);
}
}
}
return View();
}
目前,當使用拖放時我得到0: Invalid calling object
Edge和Uncaught TypeError: Illegal invocation
0: Invalid calling object
錯誤0: Invalid calling object
Uncaught TypeError: Illegal invocation
Chrome中的Uncaught TypeError: Illegal invocation
,這不是很有幫助。 當從<input>
選擇文件時,我收到以下錯誤消息: HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (XHR)POST - http://localhost:51348/Upload/Home
HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier). (XHR)POST - http://localhost:51348/Upload/Home
有幾個問題。 1) jQuery.ajaxSettings.traditional = true
需要將數組傳遞給IEnumarable
2) '@Url.Action("Home", "Upload")'
- > '@Url.Action("Upload")'
這可能會解決一些問題。 然而,調用是成功的但IEnumarable
總是空的並且修復到這里: 如何使用ajax上傳文件到asp.net mvc控制器動作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.