A friend-encapsulated ASP. NET File Upload method, asp.net File Upload Method
After years of development in asp.net, I feel that the encapsulation is good !!!
The Code is as follows:
C # code
- # Region File Upload Method
- /// <Summary>
- /// File Upload Method
- /// </Summary>
- /// <Param name = "myFileUpload"> upload Control ID </param>
- /// <Param name = "allowExtensions"> extended file name types that can be uploaded, such as string [] allowExtensions = {". doc ",". xls ",". ppt ",". jpg ",". gif "};</param>
- /// <Param name = "maxLength"> maximum upload size, in MB </param>
- /// <Param name = "savePath"> Save the directory of the file. Note that it is an absolute path, such as Server. MapPath ("~ /Upload/"); </param>
- /// <Param name = "saveName"> saved file name. If it is "", save it as the original file name </param>
- Private void Upload (FileUpload myFileUpload, string [] allowExtensions, int maxLength, string savePath, string saveName)
- {
- // Whether the file format allows upload
- Bool fileAllow = false;
- // Check whether a file exists
- If (myFileUpload. HasFile)
- {
- // Check the file size. The ContentLength value is the byte, which is divided by 2 times and 1024 when converted to M.
- If (myFileUpload. PostedFile. ContentLength/1024/1024> = maxLength)
- {
- Throw new Exception ("only files smaller than 2 MB can be uploaded! ");
- }
- // Get the extended File Name of the uploaded file and convert it to lowercase letters
- String fileExtension = System. IO. Path. GetExtension (myFileUpload. FileName). ToLower ();
- String tmp = ""; // The suffix of the file that can be uploaded
- // Check whether the extension file name meets the specified type
- For (int I = 0; I <allowExtensions. Length; I ++)
- {
- Tmp + = I = allowExtensions. Length-1? AllowExtensions [I]: allowExtensions [I] + ",";
- If (fileExtension = allowExtensions [I])
- {
- FileAllow = true;
- }
- }
- If (fileAllow)
- {
- Try
- {
- String path = savePath + (saveName = ""? MyFileUpload. FileName: saveName );
- // Store the file to a folder
- MyFileUpload. SaveAs (path );
- }
- Catch (Exception ex)
- {
- Throw new Exception (ex. Message );
- }
- }
- Else
- {
- Throw new Exception ("the file format does not match. The file format that can be uploaded is:" + tmp );
- }
- }
- Else
- {
- Throw new Exception ("select the file to upload! ");
- }
- }
- # Endregion
The following is the test code:
C # code
- Try
- {
- String [] ss = {". jpg", ". gif "};
- String path = Request. MapPath ("~ /Upload /");
- Upload (FileUpload1, ss, 1, path ,"");
- Label1.Text = "File Uploaded successfully! ";
- }
- Catch (Exception ex)
- {
- Label1.Text = ex. Message;
- }
I have never carefully studied Exception Handling before. Today, I found that exception handling is quite good... At least the code volume is less than if... else... Haha...
How does aspnet encapsulate a class as a dll file?
Ascx is a user control. It can be referenced directly on the page without encapsulation. When you release it, the compiler will help you compile it into a dll without worrying about code security issues.
Add reference at the top of the page
How does ASPNET upload files uploaded by the upload control to a specified directory?
Upload button to event
Protected void upload1Button_Click (object sender, EventArgs e)
{
// Proceed with uploading only if the user selected a file
If (image1FileUpload. HasFile)
{
Try
{
String fileName = image1FileUpload. FileName;
String location = Server. MapPath ("./ProductImages/") + fileName;
// Save image to server
Image1FileUpload. SaveAs (location );
}
Catch
{
StatusLabel. Text = "Uploading image failed ";
}
}
}