我使用此代码上传单个文件。
jQuery Ajax:
$(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;
}
}
}
}
现在我需要使用此代码uplaod多个文件,这是怎么做的?
答案 0 :(得分:0)
我想您只需更改Session["File"]
中的类型即可使用List<HttpPostedFileBase>
代替HttpPostedFileBase
。
正如您要求使用提供的代码执行此操作。
更改GetFile
操作的代码:
public ActionResult GetFile(HttpPostedFileBase NewsDefaultFile)
{
//Request.Files[0];
if (NewsDefaultFile != null)
{
// Retrieve the list from session or create a new one
var files = Session["Files"] == null ?
new List<HttpPostedFileBase>() :
(List<HttpPostedFileBase>)Session["Files"];
// Add the posted file
files.Add(NewsDefaultFile);
// Store the list in session
Session["Files"] = files;
}
return Content("");
}
并更改CreateNews
操作的代码:
[HttpPost]
public ActionResult CreateNews(NewsModel model)
{
if (model.NewsDefaultFile == null)
{
if (Session["Files"] != null)
{
// Retrieve in Session
var files = (List<HttpPostedFileBase>)Session["Files"];
var i = 1; // Just to change a bit the filename
foreach (var File in files)
{
if (File.ContentLength > 0)
{
var ext = System.IO.Path.GetExtension(File.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
}
}
}
}
}
答案 1 :(得分:0)
尝试如下可能有帮助。
[HttpPost]
public ActionResult GetFile(List<HttpPostedFileBase> NewsDefaultFile)
{
if (NewsDefaultFile != null)
{
Session["Files"] = NewsDefaultFile;
}
return Content("");
}
[HttpPost]
public ActionResult CreateNews(NewsModel model)
{
if (model.NewsDefaultFile == null && Session["Files"] != null)
{
// Retrieve in Session
var files = (List<HttpPostedFileBase>)Session["Files"];
var i = 1; // Just to change a bit the filename
foreach (var File in files)
{
//write your own logic here
if (File.ContentLength > 0)
{
var ext = System.IO.Path.GetExtension(File.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpeg")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = model.NewsTitle + _NewsClass.Rand() + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Image/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
if (ext == ".webm" || ext == ".mkv" || ext == ".flv" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Video/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
if (ext == ".mp3" || ext == ".ogg" || ext == "WAV" || ext == ".avi" || ext == ".mov" || ext == ".3gp" || ext == ".mp4")
{
// Just added $"_{i}" to avoid overriding saved file
string filename = _NewsClass.Rand() + model.NewsTitle + $"_{i}";
File.SaveAs(System.IO.Path.Combine(Server.MapPath(@"~/Upload/Audio/" + filename)));
model.NewsDefaultFile = filename;
model.NewsDefaultFileExt = ext;
i++;
}
}
}
}
}