. NET uploads, one select direct upload (ashx) and ByteArray upload

Source: Internet
Author: User

<%@ 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.