Solved

downloading a linked file from an asp,net website

Posted on 2011-09-08
10
376 Views
Last Modified: 2012-05-12
I am using this code to upload a file to a website and it works fine
but when i try to download the file using the code as below it shows the link like and doesnot respond when you try to open it.

'link that shows
http://localhost:54753/cbha/reviewDocuments.aspx?file=C:\cbha\docs\System%20login.docx

'code to download

 Dim hl As New HyperLink

       
        hl.ID = "file" & ii

        hl.Text = drd("d_FileToUpload") & ""
        hl.NavigateUrl = "reviewDocuments.aspx?file=" + Server.MapPath("docs\" & drd("d_FileToUpload"))
        tc.Controls.Add(hl)
        tr.Cells.Add(tc)
        tb.Rows.Add(tr)

        ftc.Controls.Add(tb)
        ftr.Cells.Add(ftc)
        Me.d_table.Rows.Add(ftr)



'code to upload
 If d_FileToUpload.HasFile Then
                Try
                    If d_FileToUpload.PostedFile.ContentLength > 4000000 Then
                        Me.Label1.Text = "File size maximum is 4mbs)- file not uploaded:"
                        Exit Sub
                    End If
                    d_FileToUpload.SaveAs(Server.MapPath("docs\" & d_FileToUpload.FileName))
                    Label1.Text = "File: " & _
                       d_FileToUpload.PostedFile.FileName & " has been uploaded<br>"
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                Label1.Text = "You have not specified a file."
0
Comment
Question by:Anthony Matovu
  • 5
  • 4
10 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 36503531
 hl.NavigateUrl = Server.MapPath("docs\" & drd("d_FileToUpload"))

what's on teh review documents page?
0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36503563
nothing opens
it only shows http://localhost:54753/cbha/reviewDocuments.aspx?file=C:\cbha\docs\System%20login.docx

in the bar where we type the URL

ANTHONY
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 36503570
change the navigateURL to what I have . . . eg: remove the reviewDocuments.Aspx part from your HL link.

can you confirm that

c:\cbha\docs\system login.docx exists?



0
 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36503669
yes it does exist
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 36503685
did you remove the reviewDocuments part?


try this:
  hl.NavigateUrl = "/docs/" + drd("d_FileToUpload")
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:Anthony Matovu
ID: 36503792
i have left like \
hl.NavigateUrl = Server.MapPath("docs\" + drd("d_FileToUpload"))
and it only shows "file///c:/cbha/docs/System%20login.docx

below when you pass a cursor over it. but does not respond to a click
a
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 36504563
Remove the server.map path.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36506101
Does reviewDocument.aspx exist? If you put a path like "C:\..." it will try to open the file on the client computer not the server. You can add code the reviewDocument.aspx so that it reads the file and sends it to client.
0
 
LVL 1

Accepted Solution

by:
Anthony Matovu earned 0 total points
ID: 36508249
thank you very much for the guide.

I created a dynamic button and used the code below and it is working

  Protected Sub btn_click(ByVal sender As Object, ByVal e As EventArgs)
        Dim a = CType(sender, Button).ID
        Dim filename As String = Mid(a, a.IndexOf("_") + 2, a.Length)
        Dim ext As String = IO.Path.GetExtension(Server.MapPath("docs\" + filename))
        Dim fname As String = Server.MapPath("docs\" + filename)
        Response.ContentType = ReturnExtension(ext)
        Response.AppendHeader("Content-Disposition", "attachment; filename=" & filename)
        Response.TransmitFile(fname)
        Response.End()
    End Sub
0
 
LVL 1

Author Closing Comment

by:Anthony Matovu
ID: 36527983
It is not a very direct solution to the problem but it is an alternative that works
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# HTTP GET method sample code 3 42
Error on link 14 40
Extracting last characters in a substring 5 21
Pagebreak issue while printing the aspx page 3 16
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now