A friend-encapsulated ASP. NET File Upload method, asp.net File Upload Method

Source: Internet
Author: User

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
  1. # Region File Upload Method
  2. /// <Summary>
  3. /// File Upload Method
  4. /// </Summary>
  5. /// <Param name = "myFileUpload"> upload Control ID </param>
  6. /// <Param name = "allowExtensions"> extended file name types that can be uploaded, such as string [] allowExtensions = {". doc ",". xls ",". ppt ",". jpg ",". gif "};</param>
  7. /// <Param name = "maxLength"> maximum upload size, in MB </param>
  8. /// <Param name = "savePath"> Save the directory of the file. Note that it is an absolute path, such as Server. MapPath ("~ /Upload/"); </param>
  9. /// <Param name = "saveName"> saved file name. If it is "", save it as the original file name </param>
  10. Private void Upload (FileUpload myFileUpload, string [] allowExtensions, int maxLength, string savePath, string saveName)
  11. {
  12. // Whether the file format allows upload
  13. Bool fileAllow = false;
  14. // Check whether a file exists
  15. If (myFileUpload. HasFile)
  16. {
  17. // Check the file size. The ContentLength value is the byte, which is divided by 2 times and 1024 when converted to M.
  18. If (myFileUpload. PostedFile. ContentLength/1024/1024> = maxLength)
  19. {
  20. Throw new Exception ("only files smaller than 2 MB can be uploaded! ");
  21. }
  22. // Get the extended File Name of the uploaded file and convert it to lowercase letters
  23. String fileExtension = System. IO. Path. GetExtension (myFileUpload. FileName). ToLower ();
  24. String tmp = ""; // The suffix of the file that can be uploaded
  25. // Check whether the extension file name meets the specified type
  26. For (int I = 0; I <allowExtensions. Length; I ++)
  27. {
  28. Tmp + = I = allowExtensions. Length-1? AllowExtensions [I]: allowExtensions [I] + ",";
  29. If (fileExtension = allowExtensions [I])
  30. {
  31. FileAllow = true;
  32. }
  33. }
  34. If (fileAllow)
  35. {
  36. Try
  37. {
  38. String path = savePath + (saveName = ""? MyFileUpload. FileName: saveName );
  39. // Store the file to a folder
  40. MyFileUpload. SaveAs (path );
  41. }
  42. Catch (Exception ex)
  43. {
  44. Throw new Exception (ex. Message );
  45. }
  46. }
  47. Else
  48. {
  49. Throw new Exception ("the file format does not match. The file format that can be uploaded is:" + tmp );
  50. }
  51. }
  52. Else
  53. {
  54. Throw new Exception ("select the file to upload! ");
  55. }
  56. }
  57. # Endregion



The following is the test code:

C # code
  1. Try
  2. {
  3. String [] ss = {". jpg", ". gif "};
  4. String path = Request. MapPath ("~ /Upload /");
  5. Upload (FileUpload1, ss, 1, path ,"");
  6. Label1.Text = "File Uploaded successfully! ";
  7. }
  8. Catch (Exception ex)
  9. {
  10. Label1.Text = ex. Message;
  11. }



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 ";
}
}
}

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.