input(type="file")+Handler(一般处理程序)上传文件简单Demo

简介:                 最近两天在做本地图片上传和网络图片读取,刚开始从kindEditor里面拿了一个,但是框架的JS确实是比较多,所以领导大人又让前台妹子拿DIV画了一个静态页面。

 


              最近两天在做本地图片上传和网络图片读取,刚开始从kindEditor里面拿了一个,但是框架的JS确实是比较多,所以领导大人又让前台妹子拿DIV画了一个静态页面。前前后后可真有时间。。。


                下面拿出核心代码来聊聊:


            首先,页面上:


             

 <form action="Handler1.ashx" method="post" enctype="multipart/form-data">  
            选择要上传的图片:<input type="file" name="fileUp" />  
            <input type="submit" value="上传" />  
     </form>  

         指定form提交的地址,method,和enctype(这个一定要写);


         接着,就是提交到handler里面的处理的方法:


          

public void ProcessRequest(HttpContext context)
        {        
            //前面一堆验证,此处省略500行。。。。

            HttpPostedFile imgFile = context.Request.Files["fileUp"];
            string newFileName = DateTime.Now.ToString("yyyy-MM-dd") + System.IO.Path.GetExtension(imgFile.FileName).ToLower();
            imgFile.SaveAs(@"F:\" + newFileName);
           
            //此处省略成功时返回的JSON数据赋值
            context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");

            //此处省略输出过程
            context.Response.End(); 

        }

         额,,先到这里吧,到现在还是比较侧重后台的东西,前面的JS被N+1个前台妹子叔叔大哥改过,才出来那个框架的效果,多多锻炼吧。。。。








目录
相关文章
|
7天前
|
Java Spring
DWR上传文件提示File uploads not supported
确保你的DWR配置和代码都正确地支持文件上传,以解决"DWR上传文件提示File uploads not supported" 错误。
10 0
|
16天前
|
安全 PHP
文件上传--Upload-labs--Pass19--move_upload_file()函数特性
文件上传--Upload-labs--Pass19--move_upload_file()函数特性
|
16天前
|
PHP
文件上传--Upload-labs--Pass02--Content-Type绕过
文件上传--Upload-labs--Pass02--Content-Type绕过
|
18天前
解决ERROR in Conflict: Multiple assets emit different content to the same filename index.html 的问题
解决ERROR in Conflict: Multiple assets emit different content to the same filename index.html 的问题
21 0
|
3月前
|
JSON Java 数据格式
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
Could not extract response: no suitable HttpMessageConverter found for ..content type [text/html...]
|
XML JSON 前端开发
ajaxfileupload上传文件和报错syntaxerror: Unexpected end of input(…)
ajaxfileupload上传文件和报错syntaxerror: Unexpected end of input(…)
195 0
ajaxfileupload上传文件和报错syntaxerror: Unexpected end of input(…)
OPA 6 - module(&quot;Create Button Test&quot;);
Created by Wang, Jerry, last modified on Nov 08, 2015
OPA 6 - module(&quot;Create Button Test&quot;);
BSP hidden form in generated html source code
BSP hidden form in generated html source code
94 0
BSP hidden form in generated html source code
OPA 13 - ok(oButton,&quot;find the Create button&quot;);
Created by Wang, Jerry, last modified on Nov 08, 2015
101 0
OPA 13 - ok(oButton,&quot;find the Create button&quot;);