Как реализовать загрузку файла в Ajax ASP.NET

Я использую схемы в качестве способа быстро понять унаследованный код. Требуется некоторая работа для создания схемы, но этого всегда платежи в конце.

Обычно я использую диаграммы классов для получения большого изображения. Иногда диаграммы последовательности и даже диаграмма потоков данных, если часть o код чрезвычайно трудно понять.

В стадии проектирования я использую диаграммы классов и часто диаграммы состояний. Диаграммы состояний прекрасны, если поведение класса не соглашается со своим состоянием.

8
задан Community 23 May 2017 в 11:47
поделиться

3 ответа

Что ж, я нашел хороший пост в блоге на Encosia которые описывают решение этой проблемы загрузки файла ASP.NET AJAX. Это действительно хорошо работает.

http://encosia.com/2007/02/23/ajax-file-downloads-and-iframes/

10
ответ дан 5 December 2019 в 14:04
поделиться

You need to have this in a separate aspx that does not use ajax. Ajax is updating existing html markup on a page at the client side. What you try here is replace the responce content on the server side before sending anything to the client.

You could try this:

Have a page called Download.aspx that contains the transmit code you already have.

In your original page, you have a javascript call that calls the download page like this:

window.location.replace('Download.aspx');
2
ответ дан 5 December 2019 в 14:04
поделиться

You can try making a handler for this work.It is more secure if you can modify well. For this work,you need to encrypt file path in your page where you put a link for the file.

<a href=\"Downloads.ashx?f={0}\" target=\"_blank\">Your link to file</a> 
//{0} -> Encrypted file path
//target = _blank force browser to download file in another window

There are lots of encryption techniques in here

In your Handler page, you need to decrypt the file path into original one so you can read it with System.IO libraries.

context.Response.ContentType = ""; //-->MimeType for your file's extension

You can specify your MimeType by Registry unless your mime-type is static as images.

string mimeType = Registry.GetValue(string.Format(@"HKEY_CLASSES_ROOT\.{0}",
                  Path.GetExtension(decryptedfilePath)), "Content Type", null).ToString();

//Then everything is ready for download

byte[] buffer = File.ReadAllBytes(decryptedfilePath);
context.Response.OutputStream.Write(buffer, 0 , buffer.Length);
context.Response.Flush();

Good Luck.

0
ответ дан 5 December 2019 в 14:04
поделиться
Другие вопросы по тегам:

Похожие вопросы: