Create a file in memory for user to download






















This is not a cross browser solution but definitely something worth looking at. For example IE limits support to data uri. It does work in Chrome now tested against v20 and v21 but not IE9 that might just be the jsFiddle, but somehow I doubt it. JavaScript uses UTF internally.

Show 19 more comments. Koray Tugay Ludovic Feltz Ludovic Feltz For AngularJS 1. This is working great for me. Other answers led to Failed: network error in Chrome. This one works well. You can always save the blob and generate a new Url later if needed. Consider adding elem. Show 9 more comments. HoldOffHunger However: there's an open bug in IE 10 and I've still seen it in 11 that throws "Access is denied" on the a.

Matt data uri is cross origin in some browsers. It won't be able to access other parts of the site Since the order of answers can change over time, it's unclear which answers were above yours when you wrote this.

Can you indicate exactly which approaches don't work in Firefox? Daniel Buckmaster Daniel Buckmaster 6, 6 6 gold badges 37 37 silver badges 54 54 bronze badges.

This works great on Chrome. How do I allow the user to specific the location of the file on disk? Wow, thanks for the easy to use library. That's not related to FileSaver. IE doesn't support the download HTML 5 tag yet and the other solutions on this page and other SO pages discussing the same problem were simply not working for me. FileSaver ftw! Show 4 more comments. I just used jquery to attach events onclick and onready and set attributes, which you can also do with vanilla JS.

The core part window. There is still the limitation of size for the data uri approach, isn't it? Beau Smith Beau Smith Danielo Danielo 3, 2 2 gold badges 25 25 silver badges 44 44 bronze badges.

It works very nicely on Chrome, but not on Firefox. It does make a file and downloads it, but the file is empty. No content.

Any ideas why? Haven't tested on IE If your download is small, this works fine: document. If you just want to convert a string to be available for download you can try this using jQuery.

Rick Rick Scape data with encodeURI might be needed as I suggested here before being able to comment: stackoverflow. Dzarek Dzarek 5 5 silver badges 9 9 bronze badges. Ludovic's answer includes this big, plus support for the other browsers. Erik 1 1 gold badge 7 7 silver badges 21 21 bronze badges.

Mostafa Mostafa 1, 16 16 silver badges 30 30 bronze badges. Text Encoder is highly experimental right now, I'd suggest avoiding or polyfilling it. What are the differences between this approach and creating a Blob? Based on Rick answer which was really helpful. I was not able to accept the suggestion. I updated the code.

Caniuse support table for Blob: Here is a Fiddle. This below function worked. Denys Rusov Denys Rusov 5 5 silver badges 6 6 bronze badges. Sweta Jain 1, 1 1 gold badge 13 13 silver badges 28 28 bronze badges. Subhan Ali Subhan Ali 1 1 1 bronze badge.

I had considered that, but from a user-friendliness point, this is disastrous. Also, the file has to be saved with a CSV extension. Try telling that to your users. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Podcast what if you could invest in your favorite developer? Who owns this outage? Building intelligent escalation chains for modern SRE. Instead of storing the data in memory and then sending it to the response stream, you can write it directly to the response stream:. The example uses the UTF-8 encoding, you should change that if you are using some other encoding.

In the ProcessRequest method you basically just write your text to context. You also need to add a Content-Disposition http header:. This Google Groups posting also notes that Response.

In most cases the user is going to click a button for the download. At this point it is best to redirect the page back to the same spot. I had data that could contain commas. The common solution is to escape that piece of data by enclosing it in quotes, while making sure to also escape quotes that could also be a part of the data. One rub I came against and a warning when writing CSV, excel will not like you if you put spaces trailing your commas. Below will cause a problem for excel.

In excel the first quote will become part of the data and consequently than separate at the embedded comma. Spaces bad.

NET and C. 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. Create text file and download Ask Question. Asked 10 years, 10 months ago. Active 4 years, 9 months ago. Viewed 68k times. Write "This is the content" ; How do I then download this file? EDIT: It was combination of answers which gave me my solution.

WriteLine "Line 1" ; Response. Today ; Response. OutputStream, Encoding. Write oStringWriter. End ;. Improve this question. Add a comment. Active Oldest Votes. WriteLine "Line 1" ; tw. WriteLine "Line 2" ; tw. WriteLine "Line 3" ; tw. ToArray ; ms. Close ; Response. Clear ; Response. BinaryWrite bytes ; Response.

Improve this answer. Guffa Guffa k gold badges silver badges bronze badges. Hi Guffa - how do I then get the response stream to download though? Klaus Byskow Hoffman has some examples in his answer, but you don't have to use a HTTP handler to return the response, it works with a regular page also. You also need to add a Content-Disposition http header: context.



0コメント

  • 1000 / 1000