dongteng

Aps.net中基于bootstrapt图片上传插件的应用


在最近的项目中需要使用一个图片上传的功能,而且是多张图片同时上传到服务器的文件夹中,将图片路径存放在数据库中。为了外观好看使用了bootstrapt插件。插件下载地址:   http://www.jq22.com/jquery-info5231

 

index.html中的代码:

 

<script>
  //初始化函数
    $("#file-1").fileinput({
        uploadUrl: \'image/Handler.ashx\', // you must set a valid URL here else you will get an error
        allowedFileExtensions : [\'jpg\', \'png\',\'gif\'],
        overwriteInitial: false,
        maxFileSize: 1000,
        maxFilesNum: 10,
        //allowedFileTypes: [\'image\', \'video\', \'flash\'],
        slugCallback: function(filename) {
            return filename.replace(\'(\', \'_\').replace(\']\', \'_\');
        }
    });
   
    </script>

很多博友在uploadUrl的参数设置中总是表达不清除,使用什么MVC接受图片的信息,现在MVC使用已经下降,我使用一般处理程序完成了图片得保存。

  public class Handler : IHttpHandler
    {
        MethodUpload md = new MethodUpload();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";
            // context.Response.Write(md.SaveFile(context));
            addFile();
        }
        /// <summary>
        /// 添加图片的方法
        /// </summary>
        public void addFile()
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            try
            {
                for (int iFile = 0; iFile < files.Count; iFile++)
                {
                    ///\'检查文件扩展名字
                    HttpPostedFile postedFile = files[iFile];
                    string fileName, fileExtension;
                    fileName = System.IO.Path.GetFileName(postedFile.FileName);
                    if (fileName != "")
                    {
                        fileExtension = System.IO.Path.GetExtension(fileName);
                        string path = System.Web.HttpContext.Current.Request.MapPath("images/");
                                            
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        postedFile.SaveAs(path + fileName);                      
                    }
                }                        
            }
            catch (System.Exception Ex)
            {
              
            }
        }



        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-10-17
  • 2021-11-30
  • 2021-07-09
  • 2021-07-25
  • 2021-12-09
  • 2021-06-23
猜你喜欢
  • 2021-10-17
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案