我使用此代码上传单个文件。
jQuery 阿贾克斯:
$(document).ready(function () {
$('input[type=file]').change(function () {
$(this).simpleUpload("/Admin/News/GetFile", {
start: function (file) {
//upload started
console.log("upload started");
},
progress: function (progress) {
//received progress
console.log("upload progress: " + Math.round(progress) + "%");
},
success: function (data) {
//upload successful
console.log("upload successful!");
console.log(data);
},
error: function (error) {
//upload failed
console.log("upload error: " + error.name + ": " + error.message);
}
});
});
});
简单的上传脚本。
控制器 :
public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile)
{
//Request.Files[0];
if (NewsDefaultFile != null)
{
Session.Add("File", NewsDefaultFile);
}
return Content("");
}
[HttpPost]
public ActionResult CreateNews(NewsModel model)
{
if (model.NewsDefaultFile == null)
{
var File = (HttpPostedFileBase)Session["File"];
if (File.ContentLength > 0)
{
var ext = System.IO.Path.GetExtension(File.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
{
string filename = model.NewsTitle + _NewsClass.Rand();
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
}
if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
string filename = _NewsClass.Rand() + model.NewsTitle;
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
}
if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
string filename = _NewsClass.Rand() + model.NewsTitle;
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
}
}
}
}
现在我需要使用此代码上传多个文件,该怎么做?