Asp.net automatically determines the extension of the file to be saved Based on the MIME type during file download.

Source: Internet
Author: User

Introduction

When you use WebClient to download remote resources, you will often encounter a URL similar to this:

Http://www.uushare.com/filedownload? User = icesee & id = 2205188

Http://www.guaishow.com/u/luanfujie/g9675/

We don't know whether the Url represents a webpage or a type of file.

Although some URLs have extensions, they may be incorrect extensions. For example, you can mark a gif file with a jpg extension.

If we cannot correctly determine the type of the downloaded source file, it cannot be saved as the correct file format, which will cause problems for subsequent operations and manual reading.

Fortunately, WebRequest can provide MIME information for the download source, which allows us to determine the actual file format and determine the final storage extension. (What is MIME ?)

Create a MIME ing dictionary
The first thing we need to do is to create a MIME ing Dictionary of the MIME type to its corresponding extension.

I found a list of MIME types from the Internet, converted them into program code through regular expressions, and pasted them into the program:

The amount of code converted by regular expressions is very large.
Note that there are many data with the same MIME type but different extensions. When we add the data to the dictionary, we ignore unnecessary records, for example, if the three highlighted parts are of the audio/x-aiff type, the last two extension names will not be added to the dictionary or used in subsequent operations.
If you think the corresponding extension of some types is not the most common corresponding type, you have to manually adjust the code. (This is the case below, for example, text/html corresponds to the dhtml extension and image/jpeg corresponds to the jpe extension)
After the dictionary is built, you can use this method to obtain the extension of the MIME type:
String to obtain the corresponding extension (string ContentType)
{
Foreach (var f in MimeDic. Keys)
{
If (ContentType. ToLower (). IndexOf (f)> = 0) return MimeDic [f];
}
Return null;
}
The reason for using the IndexOf method is that the incoming ContentType may contain other information, such as the encoding format.
Digress: Someone complained on the Internet that garbled characters are easily generated when the WebClient downloads the webpage, and it is difficult to read the encoding format of the webpage. In fact, the ContentType of WebRequest contains MIME and encoding format information:

Generate the download file path
With the above method, we can determine the file extension through the MIME type.
Now we will write a method for generating the download file path. Its function is:
Analyzes the source Url of a file and uses the file name part as the file name of the downloaded file.
If the Url does not contain part of the file name (domain name or directory form), the file name is used as the name of the directory to download the file.
The original extension (if any) in the Url is automatically determined and replaced based on the input MIME type for the file name to be downloaded.
If a file with the same name as the downloaded file already exists in the input storage directory, rename the file until there is no file with the same name.
There are a lot of features and it is not suitable for examples, but it is still very practical, so we will share it here.
The code is:

Copy codeThe Code is as follows: string generated download file storage path (string directory, Uri, string ContentType)
{
Var ex = get the corresponding extension (ContentType );
String up = null;
String upne = null;
If (Uri. LocalPath = "/")
{
// Handle the situation where the Url is a domain name
Up = upne = Uri. Host;
}
Else
{
If (Uri. LocalPath. EndsWith ("/"))
{
// Process the directory where the Url is
Up = Uri. LocalPath. Substring (0, Uri. LocalPath. Length-1 );
Upne = Path. GetFileName (up );
}
Else
{
// Process regular URLs
Up = Uri. LocalPath;
Upne = Path. GetFileNameWithoutExtension (up );
}
}
Var name = string. IsNullOrEmpty (ex )? Path. GetFileName (up): upne + "." + ex;
Var fn = Path. Combine (storage directory, name );
Var x = 1;
While (File. Exists (fn ))
{
Fn = Path. Combine (storage directory, Path. GetFileNameWithoutExtension (name) + "(" + x ++ ")" + Path. GetExtension (name ));
}
Return fn;
}

To verify the effect, we conduct a unit test:Copy codeThe Code is as follows: [TestMethod]
Public void file name generation test ()
{
Var d = @ "C: \ Users \ Public \ Downloads ";
// File in gif format, which can be downloaded normally
Assert. areEqual (@ "C: \ Users \ Public \ Downloads \ 35ad5275ed17904d4a2d40f3dacea80b.gif", generate the download file storage path (d, new Uri ("/upload/2009-11/20091112231022422. gif ")," image/gif "));
// The extension in the url is gif, but the MIME type is actually image/jpeg resources. The downloaded extension is jpe, because the corresponding extension stored in the MimeDic dictionary is jpe.
Assert. areEqual (@ "C: \ Users \ Public \ Downloads \ 35ad5275ed17904d4a2d40f3dacea80b. jpe "to generate the storage path of the downloaded file (d, new Uri ("/upload/2009-11/20091112231022422. gif ")," image/jpeg "));
// A url with parameters. The downloaded extension is dhtml, because the corresponding extension stored in the MimeDic dictionary is dhtml.
Assert. AreEqual (@ "C: \ Users \ Public \ Downloads \ filedownload. dhtml", generate the download file storage path (d, new Uri ("http://www.uushare.com/filedownload? User = icesee & id = 2205188 ")," text/html "));
// A webpage url in directory format with no exact file name.
Assert. areEqual (@ "C: \ Users \ Public \ Downloads \ g9675.dhtml", generate the download file storage path (d, new Uri ("http://www.guaishow.com/u/luanfujie/g9675/"), "text/html "));
// Domain name format
Assert. areEqual (@ "C: \ Users \ Public \ Downloads \ www.g.cn. dhtml, generate the download file storage path (d, new Uri ("http://www.g.cn/"), "text/html "));
Assert. areEqual (@ "C: \ Users \ Public \ Downloads \ g.cn. dhtml, generate the download file storage path (d, new Uri ("http://g.cn"), "text/html "));
}

File Download
Everything is ready, and we have nothing to worry about. Let's complete the download method:Copy codeThe Code is as follows: // <summary>
/// Download the object to the specified directory and return the path of the object to be downloaded.
/// </Summary>
/// <Param name = "Uri"> URL </param>
/// <Param name = "storage directory"> storage directory. If a file with the same name as the file to be downloaded already exists in the directory, it is automatically renamed. </param>
/// <Returns> path of the file to be downloaded </returns>
Public string download file (Uri, string storage directory)
{
Var q = WebRequest. Create (Uri). GetResponse ();
Var s = q. GetResponseStream ();
Var B = new BinaryReader (s );
Var file = generate the download file storage path (storage directory, Uri, q. ContentType );
FileStream fs = new FileStream (file, FileMode. Create, FileAccess. Write );
Fs. Write (B. ReadBytes (int) q. ContentLength), 0, (int) q. ContentLength );
Fs. Close ();
B. Close ();
S. Close ();
Return file;
}

The code is very simple, so let's just talk about it. Let's complete the final test:Copy codeThe Code is as follows: [TestMethod]
Public void File Download test ()
{
Var d = @ "C: \ Users \ Public \ Downloads ";
// First download
Assert. AreEqual (@ "C: \ Users \ Public \ Downloads \ filedownload. dhtml", download the file (new Uri ("http://www.uushare.com/filedownload? User = icesee & id = 2205188 "), d ));
// For the second download, an object with the same name is automatically renamed.
Assert. AreEqual (@ "C: \ Users \ Public \ Downloads \ filedownload (1). dhtml", download file (new Uri ("http://www.uushare.com/filedownload? User = icesee & id = 2205188 "), d ));
// Download a gif file.
Assert. AreEqual (@ "C: \ Users \ Public \ Downloads \ 2naqyw8.gif", download file (new Uri ("http://i38.tinypic.com/2naqyw8.jpg"), d ));
}

Conclusion
Compared with WebClient, WebRequest has better controllability. When WebClient has no solution, try to make WebRequest available.
Sample source code and the XPS version in this article are packaged and downloaded
Http://xiazai.jb51.net/200911/yuanma/asp.net_mime_down.rar
Reprinted http://skyd.cnblogs.com/

Related Article

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.