图片上传

 //上传文件
function changfile(obj, imgid) { var fileName = $(obj).val(); if (fileName.indexOf("\") != -1) fileName = fileName.substring(fileName.lastIndexOf("\") + 1, fileName.length); //开始提交 $("#form").ajaxSubmit({ url: "ashx/UploadImage.ashx?filename=" + fileName + "&folder=product ", type: "post", dataType: "text", timeout: 600000, beforeSubmit: function (formData, jqForm, options) { $("#img1").attr("src", "images/upfile.jpg"); }, success: function (data) { var imageinfo = eval('(' + data + ')'); if (imageinfo.msg == "1") { $("#div_file").append("<div class='outer' imgsrc='" + imageinfo.imagepath + "'>" + "<span class='glyphicon glyphicon-trash'></span>" + "<img src='" + imageinfo.imagepath + "' style=' 80px;height: 80px' alt='上传的图片' class='img_create' /></div>"); $(":text[name='OriginalImage']").val($(":text[name='OriginalImage']").val() + "|" + imageinfo.imagepath); $(obj).val(""); } }, error: function (data, status, e) { alert("上传失败,错误信息:" + e); $(obj).val(""); } }); }
   public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.Expires = 0;
            context.Response.CacheControl = "no-cache";
            context.Request.ContentEncoding = Encoding.GetEncoding("UTF-8");
            context.Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
            context.Response.Charset = "UTF-8";

            string fi = context.Request["fileName"];//接收文件名
            string folder = context.Request["folder"];//接收文件夹名称

            HttpFileCollection _upfile = context.Request.Files;
            StringBuilder sb = new StringBuilder();
            string strFileName = string.Empty;
            for (int i = 0; i < _upfile.Count; i++)
            {
                strFileName = _upfile[i].FileName;
                if (strFileName != "")
                {
                    FileInfo f = new FileInfo(strFileName);
                    strFileName = f.Name;
                }
                // if (Common.ReqStr("filename") != "" && strFileName == Common.ReqStr("filename"))
                //  {
                context.Response.Write(fileSaveAs(_upfile[i], folder)); break;
                //  }
            }
        }
 1  /// <summary>
 2         ///  文件上传方法
 3         /// </summary>
 4         /// <param name="_postedFile"></param>
 5         /// <param name="folder">存储文件夹</param>
 6         /// <returns></returns>
 7         public string fileSaveAs(HttpPostedFile _postedFile, string folder)
 8         {
 9 
10             try
11             {
12                 string fileName = _postedFile.FileName;
13                 string _fileExt = _postedFile.FileName.Substring(fileName.LastIndexOf(".") + 1).Trim().ToLower();
14                 //验证合法的文件
15                 string[] strArray = { "jpg", "png", "jpge", "gif", "bmp", "jpeg" };
16                 if (!strArray.Contains(_fileExt))
17                 {
18                     return "{msg: 0, imagepath: "不允许上传" + _fileExt + "类型的文件!"}";
19                 }
20                 if (_postedFile.ContentLength > 4194304)
21                 {
22                     return "{msg: 0, imagepath: "文件大小不能超过4MB!"}";
23                 }
24                 if (fileName.IndexOf(' ') > -1)
25                 {
26                     fileName = fileName.Substring(fileName.LastIndexOf(' ') + 1);
27                 }
28                 else if (fileName.IndexOf('/') > -1)
29                 {
30                     fileName = fileName.Substring(fileName.LastIndexOf('/') + 1);
31                 }
32 
33                 //string uploadDir = HttpContext.Current.Request.MapPath("~/images/");
34                 string uploadDir = "/images/";//上传地址
35                 if (!string.IsNullOrEmpty(folder))
36                 {
37                     uploadDir = "/images/" + folder + "/";
38                 }
39 
40 
41                 //检查是否有该路径没有就创建
42                 if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadDir)))
43                 {
44                     Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadDir));
45                 }
46                 string strFileExt = fileName.Substring(fileName.LastIndexOf("."));
47                 strFileExt = ".jpg";
48                 Random r = new Random();
49                 string strNewFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + r.Next(1, 9);
50                 string uploadPath = uploadDir + strNewFileName + strFileExt;
51 
52 
53                 _postedFile.SaveAs(HttpContext.Current.Server.MapPath(uploadPath));
54                 System.Drawing.Image image = System.Drawing.Image.FromStream(_postedFile.InputStream);
55                 float w = image.Width;
56                 float h = image.Height;
57                 string fileType = Path.GetExtension(fileName).ToLower();
58 
59                 return "{msg: 1, imagepath: "" + uploadPath.Replace("~/", "/") + ""}";
60             }
61             catch (Exception ex)
62             {
63                 return "{msg: 0, imagepath: "上传过程中发生意外错误!+" + ex.Message + ""}";
64             }
65         }
原文地址:https://www.cnblogs.com/demoC/p/5121244.html