简单的图片上传

 1 <body>
 2     <form id="form1" runat="server">
 3     <div>
 4        <table>   
 5             <tr> 
 6                 <td style=" 400px"> 
 7                     <input id="InputFile" style=" 399px" type="file" runat="server" /></td> 
 8                 <td style=" 80px"> 
 9                     <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /></td> 
10             </tr> 
11           
12         </table>     
13     </div>
14 
15     </form>
16 </body>

.cs代码

 1  protected void UploadButton_Click(object sender, EventArgs e)
 2         {
 3             string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名 
 4             //string uploadName = InputFile.PostedFile.FileName; 
 5             string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复 
 6             if (InputFile.Value != "")
 7             {
 8                 int idx = uploadName.LastIndexOf(".");
 9                 string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
10                 pictureName = DateTime.Now.Ticks.ToString() + suffix;
11             }
12             try
13             {
14                 if (uploadName != "")
15                 {
16 
17                     string path = Server.MapPath("~/images/");//文件夹必须存在 否则报错,创建文件夹的方法if(File.Exists(@"文件路径")) System.IO.File.Create()
18                     InputFile.PostedFile.SaveAs(path + pictureName);
19                     Response.Write("<script>alert('上传成功')</script>");
20                    
21                 }
22             }
23             catch (Exception ex)
24             {
25                 Response.Write(ex);
26             } 
27         } 

 另一种方式,使用httpHander

 1 <body>
 2    <!--  请求报文的格式发生改变了,因为form表单中加入enctype="multipart/form-data"
 3       Content-Type: multipart/form-data; boundary=---------------------------7dd34bc30222
 4    -->
 5    <form action="UploadImage.ashx" method="post" enctype="multipart/form-data">
 6       选择图片:<input type="file" name="fImage" />
 7       <input type="text" name="txtName" value="rose" />
 8       <input type="submit" value="上传" />
 9    </form>
10 </body>
using System;
using System.Web;
using System.IO;

public class UploadImage : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        
        //接收上传 的文件 HttpRequest.Files属性拿到了上传的所有文件
        HttpPostedFile pfile = context.Request.Files[0];
        if (pfile != null)
        {
            //保存到硬盘上,
            string path = context.Server.MapPath("~/Upload");
            //string fileName = Path.GetFileName(pfile.FileName);
            string fileName = context.Request.Form["txtName"];
            fileName = Path.Combine(path, fileName + ".jpg");

            pfile.SaveAs(fileName);

            context.Response.Write("上传"+Path.GetFileName(fileName)+"成功了");
            context.Response.End();//不能再向Response中写入内容,本次请求结束
        }

        context.Response.Write("上传失败了,请重新试一下");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}
原文地址:https://www.cnblogs.com/gexx/p/3612641.html