Change uncertainty to confirmation ~ The Uri text file is downloaded to a local file instead of being automatically opened by the browser.

Source: Internet
Author: User

Change uncertainty to confirmation ~ The Uri text file is downloaded to a local file instead of being automatically opened by the browser.

Back to directory

This title is a bit long. In short, it indicates whether a user can be prompted for saving a file, rather than opening it in a browser, there is a fatal problem in opening a browser, that is, if your page encoding is inconsistent with the file encoding, garbled code will be opened. In fact, this article mainly solves this problem.

The above URI format file opened directly using the browser, because the file encoding is GB2312 and web code is a UTF-8, so the text on the page becomes garbled.

After knowing the root cause of the problem, we can solve it again. First, we cannot control the file encoding on the URI. Therefore, we can only read it locally (WWW server ), then perform secondary organization and return it to the client (your browser). For a URI file, we cannot directly return it. We must read it as a stream before returning it. In fact ,. net is a simple encapsulation for us. The following is the implementation code. Let's take a look:

/// <Summary> /// download the object, synchronization supports local and URI files /// </summary> /// <param name = "path"> </param> /// <returns> </returns> public actionResult DownLoad (string path) {if (path. indexOf ("http: //") <0) {string localPath = Server. mapPath ("/" + path); string contentType = MimeMapping. getMimeMapping (localPath); return File (localPath, contentType, "download File");} else {var url = new Uri (path); HttpWebRequest request = (HttpWebRequest) System. net. webRequest. create (path); HttpWebResponse response = (HttpWebResponse) request. getResponse (); Stream stream = response. getResponseStream (); var contentType = response. contentType; // get the total File size. return File (stream, contentType, "download File ");}}

Download video:

Back to directory


How can I automatically convert a TXT file into html format when it is opened in a browser?

For struts, use <c: out value = "$ {XXX}" escapeXml = "false"/>
For example, in the JS, use the keyword ("#id”).html (value );

In the es file browser, I created a new text file in it. It seems that the default file is accidentally selected and cannot be opened now.

Elasticsearch does not display the webpage option when opening text.
Clear the cache. If it still does not work, delete and reinstall it.

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.