The FileUpload class of Asp. Net implements the upload of file instances, asp. netfileupload
This document describes how to upload files using the FileUpload class of Asp. Net. Share it with you for your reference.
The specific function code is as follows:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Web. UI;
Using System. Web;
Using System. Web. UI. WebControls;
Using System. Collections;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. IO;
Namespace CSFramework. BLL
{
/// <Summary>
/// Supported file types
/// </Summary>
Public enum UploadFileType
{
ArticleAttachment = 1,
Image = 2,
Video = 3,
All = 4
}
/// <Summary>
/// Upload File Management class
/// </Summary>
Public class CFileUpload
{
Private FileUpload _ fileUpload;
Private string _ savePath;
Private string _ LastUploadedFile = string. Empty;
Private bool _ AutoGenFileName = false;
Private bool _ AutoGenWatermark = false;
Public string LastUploadedFile {get {return _ LastUploadedFile ;}}
Private string _ Error = "";
Private string PICTURE_FILE = "Maid]";
Private string ZIP_FILE = "[.zip.rar]";
Private string MUILT_MEDIA_FILE = "[.mpeg.mpg.fla.wma]";
Private int IMG_MAX_WIDTH = 700; // specify the width
Private int IMG_MAX_HEIGHT = 0; // The height is not specified.
Private int MAX_SIZE_UPLOAD = 1024; // you can upload a file smaller than 1 MB.
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "fileUpload"> Asp.net FileUpload object </param>
/// <Param name = "savePath"> Save the directory, excluding the file name </param>
/// <Param name = "autoGenFileName"> automatically generate a file name </param>
Public CFileUpload (FileUpload fileUpload, string savePath, bool autoGenFileName, bool autoGenWatermark)
{
_ SavePath = savePath;
_ FileUpload = fileUpload;
_ AutoGenFileName = autoGenFileName;
_ AutoGenWatermark = autoGenWatermark;
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "fileUpload"> Asp.net FileUpload object </param>
/// <Param name = "savePath"> Save the directory, excluding the file name </param>
Public CFileUpload (FileUpload fileUpload, string savePath)
{
_ SavePath = savePath;
_ FileUpload = fileUpload;
}
/// <Summary>
/// Upload the rarfile
/// </Summary>
Public bool UploadRARFile ()
{
Return DoUpload (ZIP_FILE );
}
/// <Summary>
/// Upload a video file
/// </Summary>
Public bool UploadVideo ()
{
Return DoUpload (MUILT_MEDIA_FILE );
}
/// <Summary>
/// Upload an image file
/// </Summary>
Public bool UploadImage ()
{
Return DoUpload (PICTURE_FILE );
}
Public bool UploadImage (int maxWidth, int maxHeight)
{
This. IMG_MAX_WIDTH = maxWidth;
This. IMG_MAX_HEIGHT = maxHeight;
Return DoUpload (PICTURE_FILE );
}
/// <Summary>
/// Upload any supported files
/// </Summary>
Public bool UploadAnySupported ()
{
Return DoUpload (PICTURE_FILE ZIP_FILE MUILT_MEDIA_FILE );
}
/// <Summary>
/// Generate a new file name
/// </Summary>
Private string GetNewFileName (string folder, string fileName)
{
If (_ AutoGenFileName) // automatically generates a 32-bit GUID file name
{
String ext = System. IO. Path. GetExtension (fileName );
String newfile = Guid. NewGuid (). ToString (). Replace ("-", "") ext;
Return folder newfile;
}
Else
{
If (System. IO. File. Exists (folder fileName ))
{
String ext = System. IO. Path. GetExtension (fileName );
String filebody = fileName. Replace (ext ,"");
Int x = 1;
While (true) // if the file exists, generate a file with (x) at the end
{
String newfile = folder filebody "(" x. ToString () ")" ext;
If (! System. IO. File. Exists (newfile ))
Return folder filebody "(" x. ToString () ")" ext;
Else
X;
}
}
Else
Return folder fileName;
}
}
/// <Summary>
/// A file smaller than 1 MB is supported.
/// </Summary>
Private bool AllowMaxSize (int fileLength)
{
Double kb = fileLength/1024;
Return (int) kb <MAX_SIZE_UPLOAD;
}
Private bool DoUpload (string allowedExtensions)
{
Try
{
Bool fileOK = false;
If (! _ FileUpload. HasFile) throw new Exception ("no file! "); // If the upload control does not contain files, exit
// Obtain the file suffix
String fileExtension = System. IO. Path. GetExtension (_ fileUpload. FileName). ToLower ();
// Check whether the included files are allowed file suffixes
FileOK = allowedExtensions. IndexOf (fileExtension)> 0;
If (! FileOK) throw new Exception ("unsupported file format! ");
// Check the size of the uploaded file
FileOK = AllowMaxSize (_ fileUpload. FileBytes. Length );
If (! FileOK) throw new Exception ("the image file cannot be greater than" MAX_SIZE_UPLOAD.ToString () "KB! ");
Try
{
// The file exists in another directory specified by the server
String savefile = GetNewFileName (_ savePath, _ fileUpload. FileName );
If (IsUploadImage (fileExtension) // Save the image
{
System. Drawing. Image output = CImageLibrary. FromBytes (_ fileUpload. FileBytes );
// Check the image width, height, and size.
If (this. IMG_MAX_WIDTH! = 0 & output. Width> this. IMG_MAX_WIDTH)
{
Output = CImageLibrary. GetOutputSizeImage (output, this. IMG_MAX_WIDTH );
}
Bitmap bmp = new Bitmap (output );
This. CreateDir (Path. GetDirectoryName (savefile ));
Bmp. Save (savefile, output. RawFormat );
Bmp. Dispose ();
Output. Dispose ();
If (_ AutoGenWatermark)
{
WatermarkImage genWatermark = new WatermarkImage ();
GenWatermark. DrawWords (savefile, AppConfig. Current. WatermarkMain,
AppConfig. Current. WatermarkDesc, float. Parse ("0.2 "));
}
}
Else // any other files
{
This. CreateDir (Path. GetDirectoryName (savefile ));
_ FileUpload. PostedFile. SaveAs (savefile );
}
_ LastUploadedFile = savefile;
Return true;
}
Catch (Exception ex)
{
Throw new Exception ("Unknown error occurred when uploading files! "Ex. Message );
}
}
Catch (Exception ex)
{
_ Error = ex. Message;
Return false;
}
}
Private void CreateDir (string dir)
{
If (Directory. Exists (dir) = false)
Directory. CreateDirectory (dir );
}
Private bool IsUploadImage (string fileExtension)
{
Bool isImage = PICTURE_FILE.IndexOf (fileExtension)> 0;
Return isImage;
}
}
}
I hope this article will help you design your asp.net program.
How does ASPNet use FileUpLoad to upload multiple files?
Some suggestions for you:
1. there is no need to dynamically add FileUpLoad for multi-file uploads, because this kind of work does not bring much benefit, because you can place four to five FileUpLoad controls, and users generally do not need to upload many files, if there are more than five objects, you can upload them multiple times.
2. to dynamically Add a file, you must first determine whether the user has another file to upload. Therefore, you must add a BUTTON or LABEL for confirmation. For example, "upload the next file ", in the event clicked by the user, write:
FileUpLoad FUL = new FileUpLoad ();
FUL. Name = "FUL" + Convet. ToString (I); // This I is the global variable you want to define. It is used to record how many times a user clicks a control during the same upload. Click auto-increment 1 at a time. The initial value is 0.
This. Controls. Add (FUL );
I ++;
The obtained path is obtained through this I and a loop is written, so the LZ should have a clear idea.
ASPNET programming uses the FileUpload Control for File Upload
Int myPos =-1
Therefore, string strFileName = strDir. Substring (myPos); the string cannot be truncated.