Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code to determine the file format and size before uploading a picture to verify whether the file is a picture or not?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to write the code to determine whether the file format and size is a picture before uploading a picture." in the daily operation, I believe that many people have doubts about how to judge the file format and size before uploading pictures. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. Hope to answer the question of "how to write the file format and size before uploading pictures to verify whether the file is a picture code" is helpful! Next, please follow the editor to study!

Verify the file type to see if the selected file is a picture:

The copy code is as follows:

/ / File type:

Protected bool IsAllowableFileType (string FileName)

{

/ / determine the file type limit from web.config

String stringstrFileTypeLimit

StringstrFileTypeLimit = ConfigurationManager.AppSettings ["PicTureTye"] .ToString ()

/ / whether the current file extension is included in this string

Response.Write (FileName + stringstrFileTypeLimit)

If (stringstrFileTypeLimit.IndexOf (FileName.ToLower ())!-1)

{

Return true

}

Else

{

Return false

}

}

Verify the file size to see if the file exceeds the maximum limit:

The copy code is as follows:

/ / File size

Public bool IsAllowableFileSize (long FileContentLength)

{

/ / limits on file size determination from web.config

Int32 doubleiFileSizeLimit

DoubleiFileSizeLimit = Convert.ToInt32 (ConfigurationManager.AppSettings ["FileSizeLimit"])

/ / determine whether the file exceeds the limit

If (doubleiFileSizeLimit > FileContentLength)

{

Return true

}

Else

{

Return false

}

}

Here are the settings for the configuration file, which specifies the extension and size of the uploaded file.

The copy code is as follows:

The code is very simple, as long as you call these two methods before uploading, you can simply verify the file you want to upload, not only for picture upload, but also for other files, as long as you modify the configuration file according to your needs.

At this point, the study on "how to write the code to verify whether the file format and size is a picture before uploading a picture" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report