locked
Upload Files to Remote Server RRS feed

  • Question

  • User-240333334 posted

     Hi,

    This is my first post to the forums. I have been looking for an answer to my question for a couple of days now and have not yet found anything that helps so I decided to bite the bullet and post a question. I am new to .NET and SQL so I might need a little extra help.

    I am building a web based application that will be used to upload files into a folder and create a link to the uploaded file within a database. This works fine when the Upload file is within the site root directory. However, I need the upload folder to be located in a centralized location so the link to the uploaded files will be consistant no matter which website is using the information in the database.

    The below code works but puts the uploaded file into the Uploads folder located whithin the root directory. Could somebody please show me how to alter the code so it will place the files somewhere on the remote server called "nutrition-csvr" c:\Upload

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
            ' Reference the FileUpload controls
            Dim PDFUpload As FileUpload = _
                CType(DetailsView1.FindControl("PDFUpload"), FileUpload)
            If PDFUpload.HasFile Then
                ' Make sure that a PDF has been uploaded
                If String.Compare(System.IO.Path.GetExtension _
                    (PDFUpload.FileName), ".pdf", True) <> 0 Then
                    UploadWarning.Text = _
                        "Only PDF documents may be used for a category's brochure."
                    UploadWarning.Visible = True
                    e.Cancel = True
                    Exit Sub
                End If
            End If
            Const PDFDirectory As String = "~/Uploads/"
            Dim FilePath As String = PDFDirectory & PDFUpload.FileName
            Dim fileNameWithoutExtension As String = _
                System.IO.Path.GetFileNameWithoutExtension(PDFUpload.FileName)
    
            Dim iteration As Integer = 1
    
            While System.IO.File.Exists(Server.MapPath(FilePath))
                FilePath = String.Concat(PDFDirectory, _
                    fileNameWithoutExtension, "-", iteration, ".pdf")
                iteration += 1
            End While
            ' Save the file to disk and set the value of the brochurePath parameter
            PDFUpload.SaveAs(Server.MapPath(FilePath))
            e.Values("FilePath") = FilePath
    
            Dim FileUpload3 As FileUpload = _
            CType(DetailsView1.FindControl("FileUpload3"), FileUpload)
            e.Values("UploadedFile") = FileUpload3.FileBytes
        End Sub


     

    Wednesday, August 26, 2009 2:57 PM

Answers

All replies

  • User1981430305 posted

    I see your using the VS upload control.  Have you ever looked at the Telerik RadUpload control?  It's got tons more features, as well as an upload progress monitor, as well as a rich client side API.  Or for an upload control that's really impressive and can handle multiple file uploads in one selection, check out RadUpload for Silverlight.

    Wednesday, August 26, 2009 3:12 PM
  • User-821857111 posted

    Are you trying to save the files to a different machine than the one that the web sites run on?  If so, this should help: http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 26, 2009 3:13 PM
  • User-240333334 posted

     Dude that software is $999.00! I had a hard enough time convincing my boss to buy the web building software I am using now. I mean, I am sure its awesome, but lets be honest, for that amount of money it had better be.

    Wednesday, August 26, 2009 6:13 PM
  • User-240333334 posted

     Ok, I added a username and password with admin rights. Created an identity impersonation and altered the script. It now looks like this.

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
            ' Reference the FileUpload controls
            Dim PDFUpload As FileUpload = _
                CType(DetailsView1.FindControl("PDFUpload"), FileUpload)
            If PDFUpload.HasFile Then
                ' Make sure that a PDF has been uploaded
                If String.Compare(System.IO.Path.GetExtension _
                    (PDFUpload.FileName), ".pdf", True) <> 0 Then
                    UploadWarning.Text = _
                        "Only PDF documents may be used for a category's brochure."
                    UploadWarning.Visible = True
                    e.Cancel = True
                    Exit Sub
                End If
            End If
            Const PDFDirectory As String = "\\nutrition-csvr\FileUpload"
            Dim FilePath As String = PDFDirectory & PDFUpload.FileName
            Dim fileNameWithoutExtension As String = _
                System.IO.Path.GetFileNameWithoutExtension(PDFUpload.FileName)
    
            Dim iteration As Integer = 1
    
            While System.IO.File.Exists(Server.MapPath(FilePath))
                FilePath = String.Concat(PDFDirectory, _
                    fileNameWithoutExtension, "-", iteration, ".pdf")
                iteration += 1
            End While
            ' Save the file to disk and set the value of the brochurePath parameter
            PDFUpload.SaveAs(Server.MapPath(FilePath))
            e.Values("FilePath") = FilePath
    
            Dim FileUpload3 As FileUpload = _
            CType(DetailsView1.FindControl("FileUpload3"), FileUpload)
            e.Values("UploadedFile") = FileUpload3.FileBytes
        End Sub


    But it still does not work. I am getting this error:

    Failed to map the path '/nutrition-csvr/FileUploadAugust 2009-elembreak.pdf'

    Thanks for all the help!

    Thursday, August 27, 2009 11:14 AM
  • User-240333334 posted

     I fixed the problem!Laughing

    Line 29 was the problem

    PDFUpload.SaveAs(Server.MapPath(FilePath))
    


     

    I was adding the server and the MapPath to the upload file destination. This works fine if your upload file is in the same directory as your website but it could not find the remote server becasue it was looking for it here c:\intetpub\wwwroot\snp_central2\nutrition-csvr\FileUpload and not \\Nutrition-csvr\FileUpload.

    First I followed these tutorials http://www.asp.net/learn/data-access/tutorial-56-vb.aspx

    Second I created an impersonation user according to the instructions I found here http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all#Page2

    Finally I changed the above script to

    PDFUpload.SaveAs(FilePath)

    Thanks Evrybody for all your help and support!

    Skeeneyman

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 27, 2009 3:32 PM