Asp.net: how to upload images to the server,
The FileUpload control of ASP. NET can be used to upload files to the server. HoverTreeTop adds the "image reading" function. images are uploaded using FileUpload.
The following code defines the file name and size of an image to be uploaded.
The file upload function is implemented using the user control. In the HoverTreePanel project, the HTPanel \ HControl \ UCPictureAdd. ascx control,
The image files uploaded by HoverTreeTop are limited to jpg, png, and gif files. Code:
<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 = "select jpg, png, or GIF image file"; return ;}
The GetGpjImageFileExtension method is in the HoverTreeFrame project,Code:
Namespace HoverTree. hoverTreeFrame. htImage {public class HoverTreeImageTool {/// <summary> /// obtain the file suffix Based on the mime content type of the image file. If it is not gif, png or jpg image 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 obtain and verify the suffix. Reference: http://hovertree.com/texiao/h/contenttype/
The size of the uploaded file is limited to 1 MB. The Code is as follows:
If (fileUpload_hovertree.PostedFile.ContentLength> 1048576) {literal_tips.Text = "the selected file is too large. "; Return ;}
1048576 bytes is 1 M.
You can use the SaveAs Method for uploading:
fileUpload_hovertree.SaveAs(h_fullName);
H_fullName is the complete file name string.
Download source code:
Http://xiazai.jb51.net/201701/yuanma/hovertreetop.rar
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!