File download problem on DASP server

Discussion in 'ASP.NET 2.0' started by hardot, Mar 21, 2007.

  1. hardot

    hardot Developer

    My file download code works fine locally on my development server, properly opening up a Download File dialog box in IE7 with Open and Save file buttons, which both work as intended. It does not work on DASP remoteserver, however, where the dialog box displays with Find and Save buttons, instead of Open and Save, and cannot find theMIME file type. I can't figure out why not. Here's a snippet of my code:



    Response.ContentType = "application/msword";


    string fileName = Path.GetFileName(sLink);


    //do a response header to avoid transmitting a file to the client which gets the name of the current aspx file


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


    //download the file via the Open or Save dialog


    Response.TransmitFile(sLink);


    Response.End();


    The sLink var contains the entire file path, while fileName contains just the whole document name (with extension). I use the application/msword MIME type because the document to be downloaded will always be a Word document. I've tried it with Excel (application/vnd.ms-excel) as well, same problem. I've also tried using WriteFile instead of TransmitFile - same result.


    Any help appreciated.
     
  2. Try some thing like this


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ' Add standard head elements.
    DocumentCenterFunctions.addStandardHeaderElements(Me.Page)

    Dim docID As Integer = CType(Request.QueryString('docID'), Integer)
    Dim docType As String = Trim(Request.QueryString('docType'))
    Dim siteID As Integer = CType(Request.QueryString('siteID'), Integer)
    Dim url As String

    Dim conTrackDocHits As New ConnObj('DocumentCenter', 'yourStoredProck', ReturnType.NonQuery, InputType.StoredProcedure)
    conTrackDocHits.ConnectionString = DocumentCenterFunctions.SetConnectionString()

    conTrackDocHits.AddParameters('@docID', docID, SqlDbType.Int)
    conTrackDocHits.AddParameters('@siteID', siteID, SqlDbType.Int)

    conTrackDocHits.Execute()
    conTrackDocHits = Nothing

    If docID = 0 Then
    url = './default.aspx?svc=RedirectHere&page=' & PageID
    'Response.Redirect(url)
    Trace.Write(url)
    Else
    Dim path As String = Server.MapPath('your/document/' & FileID& '.' & FileType) 'get file object as FileInfo
    Dim iStream As System.IO.Stream
    ' Buffer to read 10K bytes in chunk:
    Dim buffer(10000) As Byte
    ' Length of the file:
    Dim length As Integer
    ' Total bytes to read:
    Dim dataToRead As Long
    ' Identify the file to download including its path.
    Dim filepath As String = path
    ' Identify the file name.
    ' Dim filename As String = (Request.QueryString('title').ToString + '.' + docType) + ''''
    Try
    ' Open the file.
    iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, _
    IO.FileAccess.Read, IO.FileShare.Read)
    ' Total bytes to read:
    dataToRead = iStream.Length
    Response.ContentType = 'application/octet-stream'
    Response.AddHeader('Content-Disposition', 'attachment; filename=''' + (Request.QueryString('title').ToString + '.' + docType) + '''')
    ' Read the bytes.
    While dataToRead > 0
    ' Verify that the client is connected.
    If Response.IsClientConnected Then
    ' Read the data in buffer
    length = iStream.Read(buffer, 0, 10000)
    ' Write the data to the current output stream.
    Response.OutputStream.Write(buffer, 0, length)
    ' Flush the data to the HTML output.
    Response.Flush()
    ReDim buffer(10000) ' Clear the buffer
    dataToRead = dataToRead - length
    Else
    'prevent infinite loop if user disconnects
    dataToRead = -1
    End If
    End While
    Catch ex As Exception
    ' Trap the error, if any.
    ' Response.Write('Error : ' & ex.Message)
    Finally
    If IsNothing(iStream) = False Then
    ' Close the file.
    iStream.Close()
    End If
    End Try

    End If

    End Sub

    kevinasp.com
     
  3. hardot

    hardot Developer

    Thanks, Kevin. Adding the Response.Flush() helped. Also, you've got to make sure the client browser "knows" all the standard file types.
     
  4. hardot

    hardot Developer

    Well, interestingly enough, Kevin, I had already changed my code to ContentType="application/octet-stream" and it worked fine. What I meant by "knows" the file type is that on one of my client's browsers, the user's file types options in Tools->Folder Options->File Types did not include the .doc and .xls file types, so no matter what ContentType I used in the code, IE did not recognize those two until we added them. I had thought that any Windows OS came already loaded with them, especially since they are the most common.
     
  5. if you don't know the ContentType you can use this Response.ContentType = 'application/octet-stream'
    The only difference I can see is that you don't get the application icon on the client download
    Do you see a problem doing it this way?

    kevinasp.com
     

Share This Page