<%@ WebHandler language="C #"class="Uploadimage"%>usingSystem;usingsystem.web; Public classuploadimage:ihttphandler{//File upload directory Private stringUploadfolder ="Uploadimage"; Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; Httpfilecollection Files=context. Request.Files; if(Files. Count >0) { stringPath =context. Server.MapPath (Uploadfolder); Httppostedfile file= files[0]; if(File! =NULL&& file. ContentLength >0) { stringFileName = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + context. request.form["FileName"]; stringSavepath = path +"/"+FileName; File. SaveAs (Savepath); System.Web.HttpResponse obj=context. Response; Obj. Write (FileName); } } Else{context. Response.Write ("parameter Error"); Context. Response.End (); } } Public BOOLisreusable {Get { return false; } }}
<%@ Page Language="C #"ContentType="text/html"responseencoding="gb2312"Debug="true"%><%@ Import Namespace="System.IO" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=gb2312"><title>Upload</title></Head><Body><Scriptlanguage= "C #"runat= "Server"> PublicvoidPage_Load (Object Sender,eventargs e) {if (true) {string Savetofolder= "file"; String Path=Server.MapPath (Savetofolder); //by Resquest.inputstream the stream form, converted to byte[] formStream S=Request.inputstream; byte[] BT=Streamtobytes (s); //convert byte[] form to MemoryStream form and save as PictureSystem.IO.MemoryStream Ms= NewSystem.IO.MemoryStream (BT); System.Drawing.Bitmap b= NewSystem.Drawing.Bitmap (MS); B.save (Path+ "/proj.jpg"); Response.Write (true); Response.End (); } } //This function converts the stream to byte[] Publicbyte[] Streamtobytes (Stream stream) {byte[] Bytes= New byte[Stream. Length]; Stream. Read (Bytes,0, Bytes. Length); //sets the position of the current stream as the start of the streamStream. Seek (0, Seekorigin.begin); returnbytes; }</Script></Body></HTML>
. NET uploads, one select direct upload (ashx) and ByteArray upload