ajax上传表单中的文件
请问下关于ajax上传文件的问题,主要是前台的js应该怎么调?这是后台的代码
private void UploadImg(HttpContext context)
{
int code = 0;
string msg = "";
int cout = context.Request.Files.Count;
if (cout > 0)
{
HttpPostedFile hpf = context.Request.Files;
if (hpf != null)
{
string fileExt = Path.GetExtension(hpf.FileName).ToLower();
//只能上传文件,过滤不可上传的文件类型
string fileFilt = "|.gif|.jpg|.php|.jsp|.jpeg|.png|";
if (fileFilt.IndexOf(fileExt) <= -1)
{
code = 1;
msg = "图片格式不正确";
context.Response.Write(ObjToJSON.feedBackMsgDataToJson(code, msg));
return ;
}
//判断文件大小
int length = hpf.ContentLength;
if (length > 20480000)
{
code = 2;
msg = "图片太大,请上传小鱼2M的图片";
context.Response.Write(ObjToJSON.feedBackMsgDataToJson(code, msg));
return;
}
Random rd = new Random();
string strNext = rd.Next(1, 10000) + "";
while (strNext.Length < 5)
strNext = "0" + strNext;
DateTime nowTime = DateTime.Now;
string newFileName = nowTime.ToString("yyyyMMddHHmmss") + strNext + Path.GetExtension(hpf.FileName);
if (!Directory.Exists(_filedir))
Directory.CreateDirectory(_filedir);
string fileName = _filedir + newFileName;
hpf.SaveAs(fileName);
code = 4;
msg = fileName;
context.Response.Write(ObjToJSON.feedBackMsgDataToJson(code, msg));
return;
}
}
code = 3;
msg = "上传失败";
context.Response.Write(ObjToJSON.feedBackMsgDataToJson(code, msg));
return;
}
参考 http://blog.csdn.net/fuyifang/article/details/46337479
谢谢
页:
[1]