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


    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;
                }
            }
        }
  • 相关阅读:
    Linux下搭建PHP环境的参考文章小记
    jQuery遇到问题的小记
    小程序 login
    小程序编辑器vscode
    弹性布局详解——5个div让你学会弹性布局
    vue在页面嵌入别的页面或者是视频2
    VUE设置浏览器icon图标
    遮罩层出现后不能滚动 添加事件@touchmove.prevent
    vue 在script里写页面跳转
    axios post、get 请求参数和headers配置
  • 原文地址:https://www.cnblogs.com/dongteng/p/6739941.html
Copyright © 2020-2023  润新知