ASP.NET 上傳圖片到FTP

目錄:html

  2.代碼web

  3.參考資料express

  4.IIS環境FTP配置windows

  5.使用虛擬目錄注意Server.MapPath()數組

1. 項目介紹安全

   創建FTP文件服務器與應用程序分開.服務器

   下面方法中的參數爲Stream由於使用的是fineUI,已經將流上傳,若是是其餘控件,後面有FileStream,Bitmap,byte[]之間的參考資料.ide

 

2.測試代碼測試

/// <summary>
        /// Bitmap:封裝 GDI+ 包含圖形圖像和其屬性的像素數據的位圖。 一個 Bitmap 是用來處理圖像像素數據所定義的對象。
        /// 難點:
        ///         1.Stream轉Bitmap,壓縮圖片
        ///         2.Bitmap 與 byte[] 轉換 (Bitmap轉MemoryStream,再經過ms.ToArray()轉byte[])
        ///         3.建立FTP上載數據流,寫入字節數(FTP服務器分IIS級別配置,和應用程序級別配置,兩個要一致.安全級別高的使用指定用戶,安全低的能夠因此用戶)
        ///         
        /// </summary>
        /// <param name="stream">繼承抽象類的實例(通常是FileStream,MemoryStream)</param>
        /// <param name="url">FTP地址</param>
        /// <param name="filename">服務器中的文件名(ftp://192.168.1.127/190_140/636288137130851325admin_Penguins.jpg)</param>
        public void SaveStream(Stream stream,string url,string filename)
        {
            MemoryStream ms = null;
            Stream strm = null;
            try
            {
                /// 1.Stream 轉成 Bitmap 並壓縮圖片
                Bitmap pimage = new Bitmap(stream);
                System.Drawing.Imaging.ImageFormat fromat = pimage.RawFormat;
                Bitmap bitNewPic = new Bitmap(pimage, 190, 140);
                /// 2.Bitmap 轉成MemoryStream(繼承Stream抽象類)
                ms = new MemoryStream();
                bitNewPic.Save(ms, fromat);
                /// 3.MemoryStream轉成byte[]數組 "imagebyte"
                byte[] imagebyte = new Byte[ms.Length];
                imagebyte = ms.ToArray();
                /// 4.建立FTP服務器鏈接 "reqFTP"
                string uri = filename;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential("administrator", "vtlongxing");
                reqFTP.KeepAlive = false;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.UseBinary = true;
                reqFTP.ContentLength = imagebyte.Length;
                /// 5.建立FTP服務器上載數據的流 "strm",並向"strm"寫入字節序列
                strm = reqFTP.GetRequestStream();
                strm.Write(imagebyte, 0, imagebyte.Length);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                /// 6.關閉各"流"
                strm.Close(); 
                ms.Close();
                stream.Close();
            }
        }

 

3.filestream,bety,Bitmap操做參考ui

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

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;

namespace AppBox.CRM.Gift
{
    public class ImageHelper
    {
        //byte[] 轉圖片  
        public static Bitmap BytesToBitmap(byte[] Bytes)
        {
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream(Bytes);
                return new Bitmap((Image)new Bitmap(stream));
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (ArgumentException ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        //圖片轉byte[]   
        public static byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms, Bitmap.RawFormat);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }



        /// <summary>  
        /// 將 Stream 轉成 byte[]  
        /// </summary>  
        public byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 設置當前流的位置爲流的開始  
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        /// <summary>  
        /// 將 byte[] 轉成 Stream  
        /// </summary>  
        public Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }


        /* - - - - - - - - - - - - - - - - - - - - - - - -  
         * Stream 和 文件之間的轉換 
         * - - - - - - - - - - - - - - - - - - - - - - - */
        /// <summary>  
        /// 將 Stream 寫入文件  
        /// </summary>  
        public void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 轉換成 byte[]  
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 設置當前流的位置爲流的開始  
            stream.Seek(0, SeekOrigin.Begin);

            // 把 byte[] 寫入文件  
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }

        /// <summary>  
        /// 從文件讀取 Stream  
        /// </summary>  
        public Stream FileToStream(string fileName)
        {
            // 打開文件  
            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            // 讀取文件的 byte[]  
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            // 把 byte[] 轉換成 Stream  
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
    }
}
View Code

 

4.IIS下FTP配置

(FTP服務器分IIS級別配置,和應用程序級別配置,兩個要一致.安全級別高的使用指定用戶,安全低的能夠因此用戶)

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

 

5.虛擬目錄   (使用虛擬目錄和相對路徑要用Server.MapPath())

// <virtualDirectory path="/VImage/" physicalPath="D:\Uploadfile\CRM\Image" /> iis express 配置虛擬目錄
        string strImagePathV = "~/upimage/";//虛擬目錄
        string strImagePath190_140V = "~/upimage/190_140/";//虛擬目錄
string strPath = Server.MapPath(strImagePathV + fileName);
                string strPathchange = Server.MapPath(strImagePath190_140V + fileName);
UploadImage img = new UploadImage();
                img.Save(tab1UploadImage.PostedFile.InputStream, strPathchange);

public class UploadImage
    {
        public bool Save(Stream stream, string imagename)
        {
            try
            {
                Bitmap pimage = new Bitmap(stream);
                Bitmap bitNewPic = new Bitmap(pimage, 190, 140);
                bitNewPic.Save(imagename, System.Drawing.Imaging.ImageFormat.Jpeg);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
相關文章
相關標籤/搜索