您的位置:首页 > Web前端 > JavaScript

使用MultFile.js实现多文件上传

2014-03-18 13:46 495 查看
1.<script src="@Url.Content("~/Scripts/jquery.MultiFile.js")" type="text/javascript"></script>

2. <div style="padding: 10px;">

<input type="file" id="uploadFile" name="uploadFile" class="multi" /><br />

<span id="msgContent" style="color: Red;">@ViewBag.ErrorMsg</span>

</div>

3.[HttpPost]

[ValidateInput(false)]

public ActionResult AddArticle(Article article)

{

HttpFileCollectionBase files = Request.Files;

string attachmentPath = string.Empty;

if (files.Count != 0)

{

for (int i = 0; i < files.Count; i++)

{

if (Path.GetExtension(Path.GetFileName(files[i].FileName)) == ".exe")

{

ViewBag.ErrorMsg = "不能上传格式为exe的文件!";

break;

}

if (files[i].ContentLength > 1073741824)

{

ViewBag.ErrorMsg = "文件大小最大为1G!";

}

}

if (!string.IsNullOrWhiteSpace(ViewBag.ErrorMsg))

{

return View(article);

}

//上传文件

article.CreateDate = DateTime.Now;

article.LastUpdateDate = article.CreateDate;

articleManager.AddArticle(article);

IndexHelper.AddIndex(article);

if (files.Count == 1 && files[0].ContentLength == 0)

{

//不做操作

}

else

{

for (int i = 0; i < files.Count; i++)

{

var file = files[i];

//保存成自己的文件全路径,newfile就是你上传后保存的文件    

string fileName = System.IO.Path.GetFileName(file.FileName);//上传文件名

string fileExtension = System.IO.Path.GetExtension(fileName); //上传文件的扩展名

//保存文件

string FileId = DateTime.Now.ToString("yyyyMMddhhmmss");

string newFileName = FileId + fileExtension;

Attachment attachment = new Attachment

{

CreateDate = DateTime.Now,

FileIconName = fileExtension,

FileName = fileName,

FileSize = file.ContentLength,

FileUrl = newFileName

};

attachmentManager.AddAttachment(attachment);

articleInAttachmentManager.AddArticleInAttachment(new ArticleInAttachment { ArticleID=article.ID, AttachmentID=attachment.ID });

string soureFileUrl = Server.MapPath(ConfigurationManager.AppSettings["attachPath"]) + newFileName;

file.SaveAs(soureFileUrl);

}

}

}

return RedirectToAction("Index", "Home");

}

Web.config

<system.web>

<!--上传大文件-->

<httpRuntime maxQueryStringLength="2097151" maxUrlLength="2097151" maxRequestLength="1073741824" executionTimeout="3600" requestValidationMode="2.0"/>

</system.web>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: