ASP. Upload images to FTP

Source: Internet
Author: User

Directory:

Code, references, IIS environment FTP configuration, vs2013 IIS Express virtual directory

1. Project Introduction

Establish an FTP file server separate from the application (guessing to improve IIS performance).

The parameters in the following method are stream because you are using Fineui, you have uploaded the stream, and if it is another control, there is a reference between filestream,bitmap,byte[].

2. Test code

/// <summary>        ///Bitmap: Encapsulates a GDI + bitmap that contains pixel data for a graphics image and its properties.        A Bitmap is used to manipulate the object defined by the image pixel data. ///Difficulty:///1.Stream turn bitmap, compress picture///2.Bitmap and byte[] conversion (Bitmap to MemoryStream, then through Ms. ToArray () turn byte[])///3. Create the FTP upload data stream, write the number of bytes (FTP server is configured at IIS level, and application-level configuration, two to be consistent. High security level using the specified user, the security is low can therefore users)///                 /// </summary>        /// <param name= "Stream" >inheriting an instance of an abstract class (typically Filestream,memorystream)</param>        /// <param name= "url" >FTP Address</param>        /// <param name= "filename" >file name (ftp://192.168.1.127/190_140/636288137130851325admin_Penguins.jpg) in the server</param>         Public voidSavestream (Stream stream,stringUrlstringfilename) {MemoryStream ms=NULL; Stream STRM=NULL; Try            {                ///1.Stream turn into Bitmap and compress picturesBitmap Pimage =NewBitmap (stream); System.Drawing.Imaging.ImageFormat Fromat=Pimage.                Rawformat; Bitmap Bitnewpic=NewBitmap (Pimage, the, $); ///2.Bitmap turn into MemoryStream (inherit stream abstract class)ms =NewMemoryStream ();                Bitnewpic.save (MS, Fromat); ///3.MemoryStream turn into byte[] array "Imagebyte"                byte[] Imagebyte =NewByte[ms.                Length]; Imagebyte=Ms.                ToArray (); ///4. Create an FTP server connection "REQFTP"                stringURI =filename;                FtpWebRequest reqftp; Reqftp= (ftpwebrequest) ftpwebrequest.create (NewUri (URI)); Reqftp.credentials=NewNetworkCredential ("Administrator","vtlongxing"); Reqftp.keepalive=false; Reqftp.method=WebRequestMethods.Ftp.UploadFile; Reqftp.usebinary=true; Reqftp.contentlength=Imagebyte.                Length; ///5. Create the Stream "STRM" of the FTP server upload data and write the byte sequence to "STRM"STRM =Reqftp.getrequeststream (); Strm. Write (Imagebyte,0, Imagebyte.            Length); }            Catch(Exception ex) {}finally            {                ///6. Close each "flow"STRM.                 Close (); Ms.                Close (); Stream.            Close (); }        }

3.filestream,bety,bitmap Operation Reference

http://blog.csdn.net/wangyue4/article/details/6819102

usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingsystem.web;namespaceappbox.crm.gift{ Public classImagehelper {//byte[] Turn picture         Public StaticBitmap Bytestobitmap (byte[] Bytes) {MemoryStream stream=NULL; Try{Stream=NewMemoryStream (Bytes); return NewBitmap (Image)NewBitmap (stream)); }            Catch(ArgumentNullException ex) {Throwex; }            Catch(ArgumentException ex) {Throwex; }            finally{stream.            Close (); }        }        //picture to byte[]         Public Static byte[] bitmaptobytes (Bitmap Bitmap) {MemoryStream ms=NULL; Try{ms=NewMemoryStream ();                Bitmap.save (MS, Bitmap.rawformat); byte[] Byteimage =NewByte[ms.                Length]; Byteimage=Ms.                ToArray (); returnByteimage; }            Catch(ArgumentNullException ex) {Throwex; }            finally{Ms.            Close (); }        }        /// <summary>          ///turn the Stream into byte[]/// </summary>           Public byte[] 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; }        /// <summary>          ///turn byte[] into a Stream/// </summary>           PublicStream Bytestostream (byte[] bytes) {Stream Stream=NewMemoryStream (bytes); returnstream; }        /*-------------------to----------------------------------------------  - - - - - - - - */        /// <summary>          ///write Stream to file/// </summary>           Public voidStreamtofile (Stream stream,stringfileName) {            //convert Stream to byte[]            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); //write byte[] to fileFileStream fs =NewFileStream (FileName, FileMode.Create); BinaryWriter BW=NewBinaryWriter (FS); Bw.            Write (bytes); Bw.            Close (); Fs.        Close (); }        /// <summary>          ///read Stream from File/// </summary>           PublicStream Filetostream (stringfileName) {            //Open FileFileStream FileStream =NewFileStream (FileName, FileMode.Open, FileAccess.Read, FileShare.Read); //read the file byte[]            byte[] bytes =New byte[Filestream.length]; FileStream.Read (Bytes,0, Bytes.            Length);            Filestream.close (); //convert byte[] to StreamStream stream =NewMemoryStream (bytes); returnstream; }    }}
View Code

FTP Configuration under 4.IIS

(FTP server is configured at IIS level, and application-level configuration, two to be consistent.) High security level using the specified user, the security is low can therefore the user)

Http://www.juheweb.com/Tutorials/fwq/windows/335.html

5. Virtual directory (use Server.MapPath () for virtual directories and relative paths)

//<virtualdirectory path= "/vimage/" physicalpath= "D:\Uploadfile\CRM\Image"/> IIS Express configuration virtual directory        stringSTRIMAGEPATHV ="~/upimage/";//Virtual Directory        stringstrimagepath190_140v ="~/upimage/190_140/";//Virtual Directorystringstrpath = Server.MapPath (STRIMAGEPATHV +fileName); stringStrpathchange = Server.MapPath (strimagepath190_140v +fileName); Uploadimage img=NewUploadimage (); Img. Save (Tab1UploadImage.PostedFile.InputStream, strpathchange); Public classUploadimage { Public BOOLSave (Stream stream,stringimagename) {            Try{Bitmap Pimage=NewBitmap (stream); Bitmap Bitnewpic=NewBitmap (Pimage, the, $);                Bitnewpic.save (imagename, System.Drawing.Imaging.ImageFormat.Jpeg); return true; }            Catch(Exception ex) {return false; }        }    }

ASP. Upload images to FTP

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.