瞭解C#檔案上傳類,檔案流,位元組數組等知識

來源:互聯網
上載者:User
using System;using System.IO;using System.Web;using System.Web.UI.WebControls;namespace DotNet.Utilities{    /// <summary>    /// 檔案上傳類    /// </summary>    public class FileUp    {        public FileUp()        { }        /// <summary>        /// 轉換為位元組數組        /// </summary>        /// <param name="filename">檔案名稱</param>        /// <returns>位元組數組</returns>        public byte[] GetBinaryFile(string filename)        {            if (File.Exists(filename))            {                FileStream Fsm = null;                try                {                    Fsm = File.OpenRead(filename);                    return this.ConvertStreamToByteBuffer(Fsm);                }                catch                {                    return new byte[0];                }                finally                {                    Fsm.Close();                }            }            else            {                return new byte[0];            }        }        /// <summary>        /// 流轉化為位元組數組        /// </summary>        /// <param name="theStream">流</param>        /// <returns>位元組數組</returns>        public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)        {            int bi;            MemoryStream tempStream = new System.IO.MemoryStream();            try            {                while ((bi = theStream.ReadByte()) != -1)                {                    tempStream.WriteByte(((byte)bi));                }                return tempStream.ToArray();            }            catch            {                return new byte[0];            }            finally            {                tempStream.Close();            }        }        /// <summary>        /// 上傳檔案        /// </summary>        /// <param name="PosPhotoUpload">控制項</param>        /// <param name="saveFileName">儲存的檔案名稱</param>        /// <param name="imagePath">儲存的檔案路徑</param>        public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)        {            string state = "";            if (PosPhotoUpload.HasFile)            {                if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)                {                    string MimeType = PosPhotoUpload.PostedFile.ContentType;                    if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))                    {                        string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);                        PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));                    }                    else                    {                        state = "上傳檔案類型不正確";                    }                }                else                {                    state = "上傳檔案不能大於10M";                }            }            else            {                state = "沒有上傳檔案";            }            return state;        }        /// <summary>        /// 上傳檔案        /// </summary>        /// <param name="binData">位元組數組</param>        /// <param name="fileName">檔案名稱</param>        /// <param name="fileType">檔案類型</param>        //-------------------調用----------------------        //byte[] by = GetBinaryFile("E:\\Hello.txt");        //this.SaveFile(by,"Hello",".txt");        //---------------------------------------------        public void SaveFile(byte[] binData, string fileName, string fileType)        {            FileStream fileStream = null;            MemoryStream m = new MemoryStream(binData);            try            {                string savePath = HttpContext.Current.Server.MapPath("~/File/");                if (!Directory.Exists(savePath))                {                    Directory.CreateDirectory(savePath);                }                string File = savePath + fileName + fileType;                fileStream = new FileStream(File, FileMode.Create);                m.WriteTo(fileStream);            }            finally            {                m.Close();                fileStream.Close();            }        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.