Learn about C # File upload classes, file streams, byte arrays, and more

Source: Internet
Author: User
Tags fsm
Using system;using system.io;using system.web;using system.web.ui.webcontrols;namespace DotNet.Utilities{//< summary>///File Upload class///</summary> public class Fileup {public Fileup () {}//        <summary>//Convert to byte array///</summary>/<param name= "filename" > file name </param> <returns> byte array </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>///flow into byte array///</summary>//<param name= "Thestream" > Stream </param>//<returns> byte array </returns> public byte[] Convertstreamtobytebuffer (System.IO.Str            EAM thestream) {int bi;            MemoryStream tempstream = new System.IO.MemoryStream (); try {while (bi = Thestream.readbyte ())! =-1) {TEMPSTREAM.W                Ritebyte (((byte) bi));            } return Tempstream.toarray ();            } catch {return new byte[0];            } finally {tempstream.close (); }}///<summary>//Upload file///</summary>//<param name= "Posphotouploa D "> Controls </param>//<param name=" Savefilename "> Saved filenames </param>///<param name=" Imagepa Th "> Saved file path </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 = "Incorrect upload file type";                }} else {state = "Upload file cannot be greater than 10M";        }} else {        state = "File not uploaded";        } return state; }///<summary>//Upload file///</summary>//<param name= "Bindata" > byte array </p aram>//<param name= "filename" > FileName </param>//<param name= "fileType" > File type </param&        Gt        -------------------call----------------------//byte[] by = Getbinaryfile ("E:\\hello.txt"); This.        SaveFile (By, "Hello", ". txt"); ---------------------------------------------public void SaveFile (byte[] bindata, String fileName, String filety            PE) {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 (); }        }    }}
Related Article

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.