Asp.net: how to upload images to the server,

Source: Internet
Author: User

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!

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.