SoFunction
Updated on 2025-03-09

Detailed explanation of the method of uploading pictures to the server

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

What you want to explain here is codes such as uploading images to qualified file name and file size.

The file upload function is implemented using user controls, in the HTPanel\HControl\ control in the HoverTreePanel project,

The 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 = (fileUpload_hovertree.);
 if (h_info.HtSuffix == "")
 {
 literal_tips.Text = "Please select jpg, png or gif image file";
 return;
 }

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

namespace 
{
 public class HoverTreeImageTool
 {
 /// &lt;summary&gt;
 /// Get the suffix name of the file according to the mime content type of the image file. If it is not a gif, png or jpg image file, it returns an empty string /// /h/bjag/
 /// /texiao/h/contenttype/
 /// &lt;/summary&gt;
 /// &lt;param name="contentType"&gt;&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 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 ;
 }
 }
 }
}

That is, use ContentType to get and verify the suffix name. refer to:/texiao/h/contenttype/

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

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

1048576 bytes is 1M.

Uploading with SaveAs method:

fileUpload_hovertree.SaveAs(h_fullName);

where h_fullName is the complete file name string.

Source code download:

http://xiazai./201701/yuanma/

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!