• 文件上传到Web服务器


    1.添加需要的控件

      <form id="uploderform" method="post" action="fileupload.ashx" enctype="multipart/form-data"  runat="server" >

     文件名称:  <input type="file" id="myfile" runat="server" name="myfile">

                     <input type="button"  value="上 传"  id="fileupload" name="upload">

     </form >

    2 ashx文件

     public void ProcessRequest (HttpContext context) {  

    •         HttpPostedFile _upfile = context.Request.Files["File1"];  
    •         if (_upfile == null)  
    •         {  
    •             ResponseWriteEnd(context, "4");//请选择要上传的文件   
    •         }  
    •         else  
    •         {  
    •             string fileName = _upfile.FileName;/*获取文件名: C:\Documents and Settings\Administrator\桌面\123.jpg*/  
    •             string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/  
    •             int bytes = _upfile.ContentLength;//获取文件的字节大小   
    •   
    •             if (suffix != "jpg")  
    •                 ResponseWriteEnd(context, "2"); //只能上传JPG格式图片   
    •             if (bytes > 1024 * 1024)  
    •                 ResponseWriteEnd(context, "3"); //图片不能大于1M   
    •   
    •             _upfile.SaveAs(HttpContext.Current.Server.MapPath("~/images/logo.jpg"));//保存图片   
    •             ResponseWriteEnd(context, "1"); //上传成功   
    •         }  
    •     }  
  • 相关阅读:
    第十三周助教小结
    记事本
    第十二周助教小结
    与周老师会谈之后的感想
    第十一周总结
    第十周助教总结
    听周筠老师一席话,受益匪浅
    2020软件工程作业04
    2020软件工程作业02
    2020软件工程作业01
  • 原文地址:https://www.cnblogs.com/linsu/p/2228554.html
Copyright © 2020-2023  润新知