Asp.net fileupload

Source: Internet
Author: User

/// <Summary>

  1. /// Upload a file
  2. /// </Summary>
  3. /// <Param name = "pf"> httppostedfile </param>
  4. /// <Param name = "path"> path </param>
  5. /// <Param name = "FILENAME"> output file name </param>
  6. /// <Returns> error message </returns>
  7. Public static string uploadfile (httppostedfile PF, string savepath, out string filename)
  8. {
  9. String fileext = getfileext (PF. filename );
  10. If (! Allowfileext (fileext ))
  11. {
  12. Filename = string. empty;
  13. Return "the upload file extension is not allowed ";
  14. }
  15. Else if (PF. contentlength & gt; 1048576)
  16. {
  17. Filename = string. empty;
  18. Return "the size of the uploaded file cannot exceed 1 MB ";
  19. }
  20. Filename = getnewfilename () + "." + fileext;
  21. String Path = httpcontext. Current. server. mappath ("~ "+"/"+ Savepath +"/"+ filename );
  22. PF. saveas (PATH );
  23. Return string. empty;
  24. }
  25. /// <Summary>
  26. /// New file name
  27. /// </Summary>
  28. /// <Returns> </returns>
  29. Public static string getnewfilename ()
  30. {
  31. Return datetime. Now. tostring ("mmddhhmmss ");
  32. }
  33. /// <Summary>
  34. /// Get the file extension
  35. /// </Summary>
  36. /// <Param name = "FILENAME"> file name </param>
  37. /// <Returns> extension string </returns>
  38. Static Public String getfileext (string filename)
  39. {
  40. Int Pos = filename. lastindexof (".");
  41. Return filename. substring (Pos + 1 );
  42. }
  43. /// <Summary>
  44. /// Get the file name
  45. /// </Summary>
  46. /// <Param name = "FILENAME"> </param>
  47. /// <Returns> main file name string </returns>
  48. Static Public String getfilename (string filename)
  49. {
  50. Int Pos = filename. lastindexofany (New char [] {'\', '/'});
  51. Int pos1 = 0;
  52. Int Len = pos-pos1;
  53. Return filename. substring (pos1 + 1, Len );
  54. }
  55. /// <Summary>
  56. /// File type that can be uploaded
  57. /// </Summary>
  58. /// <Param name = "Ext"> file type </param>
  59. /// <Returns> yes/no </returns>
  60. Static public bool allowfileext (string ext)
  61. {
  62. Try
  63. {
  64. String EX = "txt, xls, Doc, docx, rar, zip ";
  65. Foreach (string s in ex. Split (','))
  66. {
  67. If (ext. tolower () = S. tolower ())
  68. {
  69. Return true;
  70. }
  71. }
  72. }
  73. Catch {return true ;}
  74. Return false;
  75. }
  76. <Asp: fileupload id = "fileupload1" runat = "server" cssclass = "fileupload"/>
  77. // Upload files to the database
  78. Protected void button#click (Object sender, eventargs E)
  79. {
  80. String filename = "";
  81. String MSG = Common. uploadfile (fileupload1.postedfile, "Upload", out filename );
  82. If ("" = MSG)
  83. {
  84. Attendinfo. attachurl = filename; // succeeded
  85. Return true;
  86. }
  87. Else
  88. {
  89. Common. showmessage ("" + MSG + ""); // failed
  90. Return false;
  91. }

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.