星空网 > 软件开发 > ASP.net

AjaxFileUpload 在C#中应用

一、前台页面

<!DOCTYPE html><html 

 二、上传功能代码 upload.ashx

using System;using System.IO;using System.Web;using System.Web.Script.Serialization;public class upload : IHttpHandler {    public void ProcessRequest (HttpContext context) {    //文件大小限制10M    const int maxSize = 10485760;    //定义允许上传的文件扩展名    var ext = new[] { "rar", "zip", "gif", "jpg", "jpeg", "png", "bmp", "xls", "xlsx", "doc", "docx", "et", "wps" };    string strExt = "";    foreach (var a in ext)    {      strExt += a + "/";    }    strExt = strExt.TrimEnd('/');    string savePath = "/upLoads/";        //文件保存路径    var resp = new UploadResponse();    HttpFileCollection imgFile = context.Request.Files;    if (imgFile.Count > 0)    {      string fileExt = Path.GetExtension(imgFile[0].FileName);      //获得扩展名      if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(ext, fileExt.Substring(1).ToLower()) < 0)      {        resp.Eor = "error";        resp.Msg = string.Format("扩展名为{0}的文件不允许上传!\n只允许上传{1}格式的文件。", fileExt, strExt);      }      else      {        if (imgFile[0].InputStream.Length > maxSize)        {          resp.Eor = "error";          resp.Msg = "上传文件大小超过限制!";        }        else        {          string fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileExt;   //新的文件名          try          {            SaveFile(imgFile[0], context.Server.MapPath(savePath), fileNewName);    //保存文件            resp.Eor = "succss";            resp.Msg = "上传成功! 文件大小为:" + imgFile[0].ContentLength;            resp.ImgUrl = savePath + fileNewName;            resp.FName = fileNewName;            resp.OName = imgFile[0].FileName;          }          catch (Exception ex)          {            resp.Eor = "error";            resp.Msg = ex.ToString();          }        }      }    }    else    {      resp.Eor = "error";      resp.Msg = "请选择文件!";    }    context.Response.ContentType = "text/html";    context.Response.Write(new JavaScriptSerializer().Serialize(resp));    context.Response.End();  }  private void SaveFile(HttpPostedFile imgFile, string savePath, string fileName)  {    if (!Directory.Exists(savePath))  //判断文件存放路径是否存在    {      Directory.CreateDirectory(savePath);    }    imgFile.SaveAs(Path.Combine(savePath, fileName));  }    public class UploadResponse  {    public string Eor { get; set; }    public string Msg { get; set; }    public string ImgUrl { get; set; }    public string FName { get; set; }    public string OName { get; set; }  }    public bool IsReusable {    get {      return false;    }  }}

 三、返回的json数据调整  

AjaxFileUpload 在C#中应用

四、源代码下载地址:http://pan.baidu.com/s/1jGo1wzW

热烈欢迎大家吐槽。

 




原标题:AjaxFileUpload 在C#中应用

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

罗马尼亚人黑五购物热情高涨,eMAG预计销售强劲:https://www.kjdsnews.com/a/1659922.html
Nykaa第二财季营收150.7亿卢比,净利润飙升50%:https://www.kjdsnews.com/a/1659923.html
Allegro公告:将开启新一轮“最低价保证”活动:https://www.kjdsnews.com/a/1659924.html
日本玩具产业:重度依赖代工 销售渠道拓宽~:https://www.kjdsnews.com/a/1659925.html
L Catterton对时装品牌ETRO增资1500万:https://www.kjdsnews.com/a/1659926.html
亚马逊转化率超高的前台展示功能,总算对大批卖家开放了!:https://www.kjdsnews.com/a/1659927.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流