asp. net Upload Picture to Server Method Detailed Explanation

  • 2021-09-12 00:47:31
  • OfStack

The FileUpload control of ASP. NET can be used to upload files to the server. HoverTreeTop has added a "picture reading" function, and pictures are uploaded with FileUpload.

What should be explained here is the code of uploading pictures to limit file names and file sizes.

The file upload function is realized by user control, HTPanel\ HControl\ UCPictureAdd. ascx control in HoverTreePanel project,

Image files uploaded by HoverTreeTop are temporarily limited to jpg, png and gif. The code is:


<asp:FileUpload runat="server" ID="fileUpload_hovertree" ClientIDMode="Static" accept="image/png,image/jpeg,image/gif" />

c # code:


HtPictureInfo h_info = new HtPictureInfo();
 h_info.HtSuffix = HoverTreeImageTool.GetGpjImageFileExtension(fileUpload_hovertree.PostedFile.ContentType);
 if (h_info.HtSuffix == "")
 {
 literal_tips.Text = " Please select jpg,png Or gif Picture file ";
 return;
 }

Where the GetGpjImageFileExtension method is in the HoverTreeFrame project, the code:


namespace HoverTree.HoverTreeFrame.HtImage
{
 public class HoverTreeImageTool
 {
 /// <summary>
 ///  According to the image file's mime The content type gets the suffix name of the file, if it is not gif,png Or jpg The picture file returns an empty string 
 /// http://hovertree.com/h/bjag/viv8qlpx.htm
 /// http://hovertree.com/texiao/h/contenttype/
 /// </summary>
 /// <param name="contentType"></param>
 /// <returns></returns>
 public static string GetGpjImageFileExtension(string contentType)
 {
 switch (contentType)
 {
 case "image/jpeg":
 return "jpg";
 case "image/pjpeg":
 return "jpg";
 case "image/gif":
 return "gif";
 case "image/png":
 return "png";
 case "image/x-png":
 return "png";
 default:
 return string.Empty;
 }
 }
 }
}

That is, use ContentType to get and verify the suffix name. Reference: http://hovertree.com/texiao/h/contenttype/

Another one is to limit the size of the uploaded file, temporarily limited to 1M, and the code is as follows:


if (fileUpload_hovertree.PostedFile.ContentLength > 1048576)
 {
 literal_tips.Text = " The selected file is too large. ";
 return;
 }

1048576 bytes is 1M.

Upload can be done using SaveAs method:

fileUpload_hovertree.SaveAs(h_fullName);

Where h_fullName is the full filename string.

Source download:

http://xiazai.ofstack.com/201701/yuanma/hovertreetop.rar


Related articles: