C # File Upload class, file stream, byte array, etc,

Source: Internet
Author: User
Tags fsm

C # File Upload class, file stream, byte array, etc,

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 a 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>
/// Convert to a byte array
/// </Summary>
/// <Param name = "theStream"> stream </param>
/// <Returns> byte array </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>
/// Upload a file
/// </Summary>
/// <Param name = "PosPhotoUpload"> Control </param>
/// <Param name = "saveFileName"> saved file name </param>
/// <Param name = "imagePath"> path of the saved file </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 file type ";
}
}
Else
{
State = "the size of the uploaded file cannot exceed 10 MB ";
}
}
Else
{
State = "No File Uploaded ";
}
Return state;
}

/// <Summary>
/// Upload a file
/// </Summary>
/// <Param name = "binData"> byte array </param>
/// <Param name = "fileName"> file name </param>
/// <Param name = "fileType"> file type </param>
// ------------------- Call ----------------------
// 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 ();
}
}
}
}

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.