转自:http://www.cnblogs.com/zhongxinWang/archive/2012/12/12/2815237.html
1.AjaxFileUpload
地址:http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
<script type="text/javascript">
function ajaxFileUpload() {
$.ajaxFileUpload(
{
url: '/Home/AjaxSave',
secureuri: false,
fileElementId: 'fileToUpload',
dataType: 'json',
beforeSend: function () {
},
complete: function () {
},
success: function (data, status) {
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(data.msg);
}
}
},
error: function (data, status, e) {
alert(e);
}
}
);
return false;
}
</script>
HTML
<input id="fileToUpload" type="file" size="45" name="fileToUpload" />
<input type="button" id="buttonUpload" onclick="return ajaxFileUpload();" value="Upload" />
后台
public void AjaxSave()
{
var file = Request.Files[0];
string path = Server.MapPath("~") + "/Upfiles/" + "/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
file.SaveAs(path + Path.GetFileName(file.FileName));
}
2.Ajax文件上传
1.Index视图中添加一个上传控件,请求文件列表的js。
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-1.8.3.min.js")"></script>
<script type="text/javascript">
$(document).ready(function () {
//文件列表
$.post("/Borrow/FileList", null, function (data) {
$("#fileList").html(data);
});
//提交图片之后的ajax请求更新
$("#fileList").click(function () {
$.post("/Home/FileList", null, function (data) {
$("#fileList").html(data);
$("#fileToUpload").val(null);
});
});
});
</script>
<div>
<div id="fileList">
文件列表
</div>
<br />
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, htmlAttributes: new { enctype = "multipart/form-data", target = "msgframe" }))
{
<input id="fileToUpload" type="file" size="45" name="fileToUpload" />
<input type="submit" style="cursor: pointer;" value="上传" />
}
<iframe id="msgframe" name="msgframe" style="display: none;"></iframe>
</div>
2.控制器
public class HomeController : Controller
{
//Index
public ViewResult Index()
{
return View();
}
//上传
public ActionResult Upload()
{
//获取文件
var file = Request.Files["fileToUpload"];
string msg = "";
//保存
if (file != null && file.ContentLength > 0)
{
//文件目录
string path = Server.MapPath("~") + "Upfiles\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//文件类型验证
string[] allowExtension = { "application/x-bmp" ,"image/jpeg", "application/x-jpg", "image/gif", "application/x-png",
"application/msword","applications-powerpoint", "application/x-ppt", "application/-excel",
"application/x-xls","application/vnd.ms-excel" };
if (!allowExtension.Contains(file.ContentType))
{
msg = "上传文件类型不正确";
}
else
{
//上传文件大小验证
long len = GetDirectoryLength(path) + file.ContentLength;
if (len <= 10 * 1024 * 1024)
{
string directoryName = path + "\\" + file.FileName;
file.SaveAs(directoryName);//文件
}
else
{
msg = "每个借款项目的文件不能超过10M";
}
}
}
else
{
msg = "添加文件失败";
}
if (!string.IsNullOrEmpty(msg))
{
Response.Write("<script>alert('" + msg + "');</script>");
}
Response.Write("<script>parent.document.getElementById('fileList').click();</script>");
return View();
}
//判断目录下文件的总大小
private long GetDirectoryLength(string dirPath)
{
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
foreach (DirectoryInfo dis in di.GetDirectories())
{
len += GetDirectoryLength(dis.FullName);
}
return len;
}
//文件列表
public ActionResult FileList()
{
return PartialView();
}
}
3.Upload视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Upload</title>
</head>
<body>
<div>
</div>
</body>
</html>
补充:
<form name="formToUpload" method="post" action="..\loadjsonM\productPlan_JhJson.ashx" enctype="multipart/form-data" target="msgframe">
<input id="fileToUpload" type="file" size="45" name="fileToUpload" />
<input type="submit" style="cursor: pointer;" value="上传" />
<input name="uptype" type="hidden" value="55" />
</form>
<iframe id="msgframe" name="msgframe" style="display: none;"></iframe>
string strReturn =
@"
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type='text/javascript'>
@msg
</script>
</head>
<body>
</body>
</html>
";
context.Response.ContentType = "text/html";
context.Response.Write(strReturn.Replace("@msg", msg));
3.选中文件即上传
function upload() {
$("#fileToUpload").click();
setTimeout("checkFile()", 1000);
}
var timeout;
function checkFile() {
var filePath = $("#fileToUpload").val();
if (filePath == "" || filePath == null) {
timeout = setTimeout("checkFile()", 1000);
} else {
clearTimeout(timeout);
$("#formUpload").submit();
$("#fileToUpload").val("");
}
}
<div style="display:none;">
<form id="formUpload" name="formToUpload" method="post" action="Accessory/Upload" enctype="multipart/form-data" target="msgframe">
<input id="fileToUpload" type="file" size="45" name="fileToUpload" />
</form>
<iframe id="msgframe" name="msgframe" style="display: none;"></iframe>
</div>
Request.File文件大小限制
错误消息:超过了最大请求长度
错误原因:asp.net默认最大上传文件大小为4M,运行超时时间为90S。
解决方案:
1. 修改web.config文件可以改变这个默认值
<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
</system.web>
<configuration>
2.另一种方法是修改.NET FrameWork:
(1) 修改 C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/CONFIG 目录下的machine.config 文件。
(2) 查找 "<httpruntime" 在这一行将 maxRequestLength的值改为理想的值,比如想要8M,就输入8192.
这样,你的任何一个 web 工程都可以上传最大8M的文件。
3.顺便说下IIS中限制上传文件大小的修改方法:
(1)首先要到进程中把IIS服务关了,即把inetinfo.exe进程关了。
(2)在系统目录中找到:windows/system32/inesrv/metabase.xml”文件,找个文本编辑器打开,查找AspMaxRequestEntityAllowed="204800"这一项,这就是iis上传文件的默认大小了,默认为204800Byte,也就是200KB,将它改为需要的大小就可以了。

被折叠的 条评论
为什么被折叠?



