Aspx.cs download .txt file from url






















This article will illustrate how to create and download a text file to the user's computer. Although in the example I actually create the text file before I stream it out to the client, I feel it is important to highlight that you don't necessarily have to do this, as the file could actually exist on the file system and you may want to stream it out to the client.

If that is the case, you may need to use FileStream to read the already existing document. We first open the file for reading and we actually read the file byte for byte into a stream, then once we have the file into a stream, we then just use the Response object and download the file via the output stream. The real power in this snippet is in the lines above; by adding a header, you are telling the browser to download the file as an attachment.

Then you set the ContentType header which is added, and set your MIME type so that the browser knows what kind of file it is about to download. You can choose any of the following MIME types for the browser:.

Using this approach, you should be able to download all types files on Windows systems, but there are some issues with Macintosh systems.

Specifically, you may not be able to download files, instead they will always open up in the browser as expected. Sign in Email. Forgot your password? Search within: Articles Quick Answers Messages. Tagged as. Stats Create and download a text file from a web page. GaryWoodfine Rate me:. Please Sign up or sign in to vote. This article will demonstrate how to create a text file and view it. Introduction It is often a common requirement in a web application to have the ability to download some sort of file to the client's computer.

Using the code Although in the example I actually create the text file before I stream it out to the client, I feel it is important to highlight that you don't necessarily have to do this, as the file could actually exist on the file system and you may want to stream it out to the client.

Copy Code. Web Developer Three Nine Consulting. Experienced in Financial Services, Security and Utilities covering all areas of software development including Solution Architecture, software design and product development. Expertise in full stack software development. Alan Oliveira Jun Member Jun Manoj Nathwani Feb Janilane Aug Tory Netherton Jun GaryWoodfine Mar If you call the method again and pass it the same file name, the existing file is completely overwritten.

However, after you've created a file you often want to add new data to the end of the file. You can do that using the AppendAllText method of the File object. In the website, make a copy of the UserData. This code has one change in it from the previous example. The methods are similar, except that AppendAllText adds the data to the end of the file.

Even if you don't need to write data to a text file, you'll probably sometimes need to read data from one. To do this, you can again use the File object. You can use the File object to read each line individually separated by line breaks or to read individual item no matter how they're separated. This procedure shows you how to read and display the data that you created in the previous example. The code starts by reading the file that you created in the previous example into a variable named userData , using this method call:.

The code to do this is inside an if statement. When you want to read a file, it's a good idea to use the File. Exists method to determine first whether the file is available.

The code also checks whether the file is empty. The body of the page contains two foreach loops, one nested inside the other. The outer foreach loop gets one line at a time from the data file. In this case, the lines are defined by line breaks in the file — that is, each data item is on its own line.

The inner loop splits each data line into items fields using a comma as a delimiter. Based on the previous example, this means that each line contains three fields — the first name, last name, and email address, each separated by a comma. The code illustrates how to use two data types, an array and the char data type. The array is required because the File. ReadAllLines method returns data as an array. The char data type is required because the Split method returns an array in which each element is of the type char.

For information about arrays, see Introduction to ASP. You can use Microsoft Excel to save the data contained in a spreadsheet as a comma-delimited file. When you do, the file is saved in plain text, not in Excel format. Each row in the spreadsheet is separated by a line break in the text file, and each data item is separated by a comma.

You can use the code shown in the previous example to read an Excel comma-delimited file just by changing the name of the data file in your code. To delete files from your website, you can use the File. Delete method. This procedure shows how to let users delete an image. Important In a production website, you typically restrict who's allowed to make changes to the data.

For information about how to set up membership and about ways to authorize users to perform tasks on the site, see Adding Security and Membership to an ASP.

This page contains a form where users can enter the name of an image file. They don't enter the. The code reads the file name that the user has entered and then constructs a complete path. To create the path, the code uses the current website path as returned by the Server. MapPath method , the images folder name, the name that the user has provided, and ". To delete the file, the code calls the File.

Delete method, passing it the full path that you just constructed. At the end of the markup, code displays a confirmation message that the file was deleted. Enter the name of the file to delete and then click Submit. If the file was deleted, the name of the file is displayed at the bottom of the page. The FileUpload helper lets users upload files to your website. The procedure below shows you how to let users upload a single file. Add the ASP.

Try adding a Response. Flush after your write statement. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Download text as file in ASP. NET Ask Question.

Asked 8 years, 9 months ago. Active 2 years, 4 months ago. Viewed 41k times. Clear ; Response. Append output ; sb. Write sb. Rui Jarimba Vijay Vijay 1 1 gold badge 3 3 silver badges 15 15 bronze badges.

Could you solve someones problem if he would tell you that "it's working on some pages and not working at all on other pages" without telling you what's not working? Add a comment. Active Oldest Votes. ToString ; Response. ClearHeaders ; Response. AppendHeader "Content-Length", text. Write text ; Response. Dennis T --Reinstate Monica-- 5 5 silver badges 19 19 bronze badges.



0コメント

  • 1000 / 1000