how to save file from database to user's local disc using asp.net(VB)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simonyong
    New Member
    • Jul 2008
    • 28

    how to save file from database to user's local disc using asp.net(VB)

    Hello, anyone
    I had search for few days with how to save file when user choose a file name from listbox and i will search the file from database and user can save it into their desktop

    what I had done currently is uploading a file to database but totally no idea about how to "download" from database

    the following is my codes to upload file to database:
    [code=vbnet]
    If Not (myFile.PostedF ile Is Nothing) Then

    OpenConn()
    Dim intFileNameLeng th As Integer, bytData() As Byte
    Dim objStream As System.IO.Strea m
    Dim strFileNamePath As String
    Dim strFileNameOnly As String

    intFileNameLeng th = myFile.PostedFi le.ContentLengt h
    ReDim bytData(intFile NameLength)
    objStream = myFile.PostedFi le.InputStream
    objStream.Read( bytData, 0, intFileNameLeng th)

    strFileNamePath = myFile.PostedFi le.FileName
    intFileNameLeng th = InStr(1, StrReverse(strF ileNamePath), "\")
    strFileNameOnly = Mid(strFileName Path, (Len(strFileNam ePath) - intFileNameLeng th) + 2)

    Dim cmd As OracleCommand
    cmd = New OracleCommand(" INSERT INTO test (filename, filesave) Values ('" + strFileNameOnly + "',:1)", myCN)
    cmd.CommandType = CommandType.Tex t

    Dim param As OracleParameter = cmd.Parameters. Add("filesave", OracleDbType.Bl ob) ' Bind the parameter as OracleDbType.Bl ob to command for inserting image
    param.Direction = ParameterDirect ion.Input

    param.Value = bytData ' Assign Byte Array to Oracle Parameter
    cmd.ExecuteNonQ uery()

    CloseConn()
    Label1.Text = "File Upload Success."
    End If
    [/code]
    Please help me/ giv me some direction to go, I hav no idea at all
    Thanks in advance
    Last edited by Frinavale; Sep 29 '08, 01:43 PM. Reason: added [code] tags
  • shansund
    New Member
    • Sep 2008
    • 3

    #2
    For txt File

    Step 1:
    [code=vbnet]
    Response.Append Header("Content-Disposition", "attachment ; filename=" + fileName + "." + "txt")[/code]

    fileName -> Give the File Name which you download

    Step2:
    Set the Content type
    [code=vbnet]
    Response.Conten tType = "applicatio n/x-download"[/code]

    Step 3:
    [code=vbnet]
    Response.Charse t = ""
    Response.WriteF ile(<here Path>)
    Response.End()
    Response.Flush( )[/code]
    Last edited by Frinavale; Sep 29 '08, 01:45 PM. Reason: added [code] tags

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by shansund
      For txt File

      Step 1:
      [code=vbnet]
      Response.Append Header("Content-Disposition", "attachment ; filename=" + fileName + "." + "txt")[/code]

      fileName -> Give the File Name which you download

      Step2:
      Set the Content type
      [code=vbnet]
      Response.Conten tType = "applicatio n/x-download"[/code]

      Step 3:
      [code=vbnet]
      Response.Charse t = ""
      Response.WriteF ile(<here Path>)
      Response.End()
      Response.Flush( )[/code]
      I'm just going to explain what Shansund was getting at here.
      What you need to do is create an ASPX page that will not display a web page. Instead this ASPX page will send the content of the file that you retrieve from your database to the user's.

      To do this you need to add an ASPX page to your project and delete any content that is automatically generated.

      Make sure you leave the <%@Page ... %> directive at the top of the page (this links to your back end server code)

      From there you need to set the page's content type to the file type that you are sending to the person (so that the browser knows what to expect as content since it's not going to be a web page).

      Then in your Page Load event you want to retrieve the file from the database and write it to your Response stream (this is sent to the browser) and flush this so that it is sent.

      Now in your main ASPX page put a link to the one you just added...when the click the link (or button) it will load the new aspx page and the content will be downloaded.

      -Frinny

      Comment

      Working...