Domanda

Sto cercando di all'utente la Response.TransmitFile () per un download rapido. Ho letto una serie di messaggi su questione e il mio metodo basato off blog di Rick Strahl http://www.west-wind.com/weblog/posts/76293.aspx

L'unica differenza (che posso dire) è che sto rivolge ad un file fisico al di fuori della directory virtuale. Questo codice è chiamato in un RadGrid ajaxified ... Mi chiedo se il Response.TransmitFile non funziona con l'Ajax chiamate? Ecco il mio frammento di codice:

            // Get the physical Path of the file
            string docFilePath = (string)args.AttachmentKeyValues["DocFilePath"];

            // Create New instance of FileInfo class to get the properties of the file being downloaded
            FileInfo file = new FileInfo(docFilePath);

            // Checking if file exists
            if (file.Exists)
            {
                Response.ClearContent();

                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);


                Response.AddHeader("Content-Length", file.Length.ToString());


                Response.ContentType = ReturnExtension(file.Extension.ToLower());


                Response.TransmitFile(file.FullName);

                Response.End();
            }

Si veda il sistema conosce il file esiste ... si arriva fino alla Response.End () senza errori ... continua poi l'applicazione corretta ... Tranne non c'è nessun download rapido.

Il metodo ReturnExtension viene sollevato da un altro sito (scusate non ricordo dove!) Come segue

    string ReturnExtension(string fileExtension)
    {
        // In the long run this should go in a class
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
È stato utile?

Soluzione

Il problema è che non riesco a fare Response.TransmitFile () da una chiamata AJAX. Dopo aver letto un paio di blog che uso il postback asincrono per impostare lo src di un iframe invisibile. L'iframe invia il file nel suo evento di caricamento.

Altri suggerimenti

Direi che il codice facendo la trasmissione non ha i diritti per aprire quel file. Puoi chiamare TransmitFile con un handle di file già aperto? Che dovrebbe funzionare meglio.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top