Belius' Blog

Il blog di Simone Belia
posts - 16, comments - 140, trackbacks - 80

ASP.NET AJAX File Download

Molte volte è necessario permettere agli utenti di scaricare dei file dalle nostre applicazioni ASP.NET, in maniera un pò più elaborata del semplice link alla risorsa. Mettete per esempio che il file da scaricare non esista, ma viene generato durante il postback, oppure mettete che per sicurezza i file stanno all'interno di una cartella del server non raggiungibile tramite Http.

Le metodologie per venire incontro a queste problematiche sono state documentate tantissime volte, ma se voglio utilizzarle con AJAX si devono utilizzare dei trucchetti, proprio perchè, come per l'upload di file, AJAX non permette il download asincrono delle risorse....

In questo post troverete uno di questi trucchetti...e spero che vi faccia comodo come lo ha fatto a me!!! :-)

Il trucchetto consiste di crearsi un HttpHandler che si occupi di recuperare la risorsa e che la restituisca sottoforma di stream nella Response. Logicamente l'handler potendo accedere al contesto e quindi il QueryString e quant'altro potrà effettuare tutti suoi controlli di sicurezza prima di restituire il file.....non me ne vogliano...ma in poche parole non fate come loro  :-)

Successivamente nelle pagine dove è presente uno scriptmanager ed uno o più updatepanel si dovrà aggiungere questo frammento di codice JavaScript:

function GetFile(parm)
{
   var iframe = document.createElement("iframe");
   iframe.src = "Downloader.ashx?id=" + parm;
   iframe.style.display = "none";
  document.body.appendChild(iframe);
}

Il Javascript non fa altro che creare un tag iframe, impostare come src il nostro handler ed aggiungerlo alla pagina. In questa maniera noi possiamo tranquillamente aggiungere ad un bottone che si occupa di scaricare il file il seguente codice:


ScriptManager.RegisterClientScriptBlock(UpdatePanel1, UpdatePanel1.GetType(), "download", "GetFile('5648752168497');", true);

che "dice" allo scriptmanager della pagina di eseguire la funzione Javascript GetFile passandogli come parametro (in questo esempio un numerone di prova) un qualcosa che vi permetta di capire quale file scaricare.

 

Il codice di esempio lo potete scaricare da qui

Print | posted on venerdì 12 ottobre 2007 11.55 | Filed Under [ ASP.NET AJAX Tips ]

Feedback

Gravatar

# re: ASP.NET AJAX File Download

Personalmente non mi piacciono molto gli iFrame, e se li uso è perchè proprio non ho alternative.
Io personalmente avrei preferito mettere l'url del httphandler nella proprietà href di un link e avrei valorizzato il target a _blank in modo che lo apra in una nuova pagina e quindi non avrei problemi per il download.

Altra soluzione, se lo devi invocare da server perchè nel client non hai l'id, ritornerei una funzione del tipo

string script = @"window.open('Downloader.ashx?id=5648752168497"','_blank','');";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), this.ClientID, script, true);

Almeno così ti eviti anche di avere la tua funzione javascript che ti crea gli iFrame ;)
12/10/2007 12.33 | Andrea Dottor
Gravatar

# re: ASP.NET AJAX File Download

....e ti posso dire che hai perfettamente ragione riguardo agli IFRAME....in effetti la prima implementazione che avevo fatto era proprio quella suggerita da te.....il cliente però come al solito ha smontato il tutto chiedendomi:

"...ma non è possibile evitare che agli utenti compaia il blocco dei popup quando scaricano il file..."?

....ed ecco giustificati gli iframe :-)

Grazie del tuo commento comunque!!!!
Belius
12/10/2007 12.51 | Simone Belia
Gravatar

# re: ASP.NET AJAX File Download

Mitico!
Sembra che mi hai letto nel pensiero, avevo esattamente questa necessità!
12/10/2007 13.18 | Alessandro Sorcinelli
Gravatar

# re: ASP.NET AJAX File Download

far fare le cose ad un iframe nascosto è sempre stato l'AJAX dei poveri... :) adesso che FFox lo supporta lo si fa un po' più a cuor leggero, ma qualche tempo fa restringeva l'applicazione al solo IE...
12/10/2007 16.07 | marco
Gravatar

# re: ASP.NET AJAX File Download

kraloyun
kral oyun
oyun
oyunlar
kraloyun
kral oyun
oyun
oyunlar
blog
blogcu
kido
azbuz
facebook
arkadaş
flash games
fotoput
youtube.
31/05/2008 20.16 | kraloyun
Gravatar

# Japanese teens in image clubs.

Japanese teens and fashion in tokyo. Naked japanese teens. Free japanese teens.
Gravatar

# Real incest stories.

Incest girl. Mom incest. Free incest stories. Incest erotic stories. Brother sister incest.
22/06/2008 23.07 | Taboo incest forum.
Gravatar

# re: ASP.NET AJAX File Download

yar yar
25/06/2008 23.56 | sohbet
Gravatar

# Big titts.

Big titts.
Gravatar

# Free incest porn gallerie.

Lesbian incest porn. Free incest porn.
09/07/2008 4.37 | Incest porn.
Gravatar

# Porn.

Porn games. Russian porn. Free gay porn. Free porn mpegs. Porn. Free porn. Ebony porn.
05/01/2009 16.50 | Porn.
Gravatar

# Free xxx.

Free xxx.
07/01/2009 17.19 | Free xxx.
Gravatar

# Taboo incest forum.

Father daughter incest. Incest storys. Free incest movies. Incest story. Incest bbs. Free erotic incest stories. Gay incest.
16/01/2009 8.31 | Incest forum.
Gravatar

# Milf.

Milf.
21/01/2009 18.56 | Milf.
Gravatar

# re: ASP.NET AJAX File Download

PDF to TIFF Converter is a professional PDF to images program which let you convert PDF to TIFF and lots of image formats with high quality, such as JPEG, PNG, GIF, BMP, PCX, TGA. As to convert PDF , you can freely set output encoder, including lossless, RLE, FAX G3, etc. Using this PDF to TIFF Converter, you alos can choose colorful, grey and black/white as output color. Reset resolution is applicable as well.
PDF to XLS Converter
PDF to EPUB Converter
Convert PDF to GIF
19/08/2010 6.27 | taoruankeji
Gravatar

# re: ASP.NET AJAX File Download

[b][url=http://www.watchesmall.net/]replica watches[/url][/b]
[b][url=http://www.watchestrade.com/Christian-Dior.html]sell christian dior watches[/url][/b]
[b][url=http://www.watchestrade.com/Breitling.html]sell breitling watches[/url][/b]
01/09/2010 8.53 | DF
Gravatar

# The Guide of PDF to Epub

Welcome to PDF to ePub Converter

The PDF to ePub converter is a perfect and professional software to help you convert PDF to ePub. Meanwhile the PDF to ePub Converter enables you to convert PDF to ePub and ePub to PDF. Many kinds of formats such as HTML, XML, TEXT, Gif, JPEG can be converted by this powerful PDF to ePub converter. And you can use this PDF to ePub Converter to parse PDF file(include all text, image) and rebuild it. Then you will get the perfect output ePub files.

By the way, the another PDF to EPUB Converter,after conversion by it you can enjoy the eBooks with iPad, iPhone, iPod Touch, Sony Reader, iRex Digital Reader 1000, PocketBook Reader and so on. Meanwhile, the converter suppports converting in batches and with the intuitive and clear interface, it is very easy even for the beginner. So you can have a try of this excellent PDF to EPUB converter. It will benefit you a lot!.
08/03/2011 3.30 | killua
Gravatar

# re: ASP.NET AJAX File Download

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. cheap MLB Jerseys I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.
28/03/2011 11.52 | fzqhlzw
Gravatar

# re: ASP.NET AJAX File Download

アバクロンビー&フィッチ(Abercrombie&Fitch)アバクロ本国アメリカではハリウッドスターもアバクロ 店舗愛用する人気のブランド、Abercrombie&Fitchアバクロ 銀座アバクロのメンズダウンジャケットです。アバクロ 銀座 セールフード付きで、アバクロ 銀座 価格ジップの部分は比翼の付きのボタンになっているのでアバクロ 通販防寒対策はバッチリアバクロ サイズ★左腕にはロゴのアップリケが付いています。アバクロ福岡軽くてあたたかいダウンジャケットは寒い冬の必需品です。少量のみの入荷です。お早めに♪♪ホリスター【ブランド】 アバクロンビー&フィッチ(Abercrombie & Fitch) アバクロホリスター【サイズ】 S/M/L/XLホリスター 日本【素材】 シェル:ポリエステル100% 裏地:ポリエステル100% フホリスター サイズィリング:フェザー65% ダウン35% フィリング2:ポリエステル100%ホリスター 公式【スタイ



09/06/2011 9.20 | luoluying

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 7 and type the answer here:

Powered by: