locked
Downloading Attachments RRS feed

  • Question

  • User750757947 posted

    Hey Guys,

    I'd like to download files of any file type from my asp.net webpage.

    I've added the following to my CodeBehind:

    Dim DownloadPath As String = "\\srfile\share$\abc.pdf"
    HttpContext.Current.Response.ContentType = "application/pdf"
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment\pdf")
    HttpContext.Current.Response.Clear()
    HttpContext.Current.Response.WriteFile(DownloadPath)
    HttpContext.Current.Response.End()

    But it throws me an error: Thread was being aborted. I also tried TransmitFile instead of WriteFile.Yell

    This is the first problem. The second one is, I'd like to be able to download any file type, not pdf only! According to http://forums.asp.net/t/1700002.aspx/1?Directly+open+a+file+with+associated+application I would have to manipulate some registry keys; isn't there another way to solve that?


    Thanks a lot Laughing

    Thursday, December 29, 2011 7:09 PM

Answers

  • User-231977777 posted

    I'm not sure if I should create a Handler for my HttpContext-Code? It's placed at the Code Behind right now.

    if your code in Code-behind , there is no need for HttpContext .just use "Response" Object directly;

    Unfortunately your Link is dead.

    No, it is not.

    Happy New Year

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 3, 2012 8:53 AM
  • User750757947 posted

    YES! It works :) Finally got it solved! Laughing

    I solved using an HTTP Handler according to the following post

    http://forums.asp.net/t/1753103.aspx/1?Using+HTTPHandler+for+file+downloading

    Call from CodeBehind

    ' open file
    Response.Redirect("OpenFile.ashx?file=" + strFilePath)


    Content of OpenFile.ashx

    Imports System.Web
    Imports System.Web.Services
    
    
    Public Class OpenFile
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Dim file As String = context.Request.QueryString("file")
    
            ' Open File
            context.Response.ContentType = "application/pdf"
            context.Response.WriteFile(file)
    
        End Sub
    
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class


    Thx!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 12, 2012 5:26 AM

All replies

  • User-231977777 posted

    Hi,

    this Line :-

    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment\pdf")

    should be like this :

    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=yourfilename.extension")

    you need to change ContentType and fileName and filePath with each file.

    refer to this Post

    Thursday, December 29, 2011 7:14 PM
  • User750757947 posted

    Hi,

    Thanks for your answer.

    I tried it, but it brings me an exeption:

    I'm not sure if I should create a Handler for my HttpContext-Code? It's placed at the Code Behind right now.

    Unfortunately your Link is dead.

    Thanks!

    PS - Happy New Year

    Tuesday, January 3, 2012 7:20 AM
  • User-231977777 posted

    I'm not sure if I should create a Handler for my HttpContext-Code? It's placed at the Code Behind right now.

    if your code in Code-behind , there is no need for HttpContext .just use "Response" Object directly;

    Unfortunately your Link is dead.

    No, it is not.

    Happy New Year

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 3, 2012 8:53 AM
  • User750757947 posted

    Yes that works so far.

    I solved it like that

    ' [ CALL FROM CODEBEHIND ]
    
    ' get filepath
    Dim strFilePath As String = ddlUploadedFiles.SelectedValue
    Session("FilePath") = strFilePath
    
    ' open file
    Response.Redirect("OpenFile.aspx")

    '-----------------------------------------


    ' [ CONTENT OF OpenFile.aspx - CODEBEHIND ]

    ' Get Path From SessionVar
    Dim strFilePath = Session("FilePath")

    ' Open File
    Response.ContentType = "application/pdf"
    Response.WriteFile(strFilePath)


    Now it directly opens my PDF-File (File1.pdf) in the browser.

    But if I go back to the first Page and try to open another PDF-File (File2.pdf), it still opens the first one (File1.pdf)! Also, I figured out by using the debugger, that the Page_Load event at the OpenFile.aspx wasn't called after the first execution. Why that? How can I solve that problem?

    Thanks a lot!

    Thursday, January 12, 2012 5:07 AM
  • User750757947 posted

    YES! It works :) Finally got it solved! Laughing

    I solved using an HTTP Handler according to the following post

    http://forums.asp.net/t/1753103.aspx/1?Using+HTTPHandler+for+file+downloading

    Call from CodeBehind

    ' open file
    Response.Redirect("OpenFile.ashx?file=" + strFilePath)


    Content of OpenFile.ashx

    Imports System.Web
    Imports System.Web.Services
    
    
    Public Class OpenFile
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Dim file As String = context.Request.QueryString("file")
    
            ' Open File
            context.Response.ContentType = "application/pdf"
            context.Response.WriteFile(file)
    
        End Sub
    
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class


    Thx!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 12, 2012 5:26 AM
  • User83408008 posted

    Hi

    its your line

    HttpContext.Current.Response.End()

    Remove it and its ok... somehow WriteFile doesnt like it when you end the Response right after ;-)

    Friday, January 13, 2012 4:15 AM