0

I am trying to upload a file and cannot get it to work. I have the correct url for the site and sub-folder. I have tried fileUploader.FileName and fileUploader.SaveAs as well. None seem to work. It is not in an UpdatePanel. Any ideas?

Code behind:

Protected Sub uploader_click(sender As Object, e As EventArgs) Handles btnUpload.Click
 If fileUploader.HasFile Then
    Dim _path As String = Path.Combine("{path to site and folder}", Server.HtmlEncode(fileUploader.PostedFile.FileName))
    Try
      fileUploader.PostedFile.SaveAs(_path)
    Catch ex As Exception
      Response.Write(ex.ToString)
    End Try
 Else
  Response.Write("No File")
 End If
End Sub 

New code, I need to get parent folder to expose the sub folder:

  Dim filePath = Path.GetFileName(fileUploader.PostedFile.FileName)
  Dim _path As String = Path.Combine(Server.MapPath("~"), "subfolder", filePath)

_path = \\{server blah}\{site}\webroot

Now no error, but no file uploaded either???

Subin Jacob
  • 4,692
  • 10
  • 37
  • 69
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • Define not working? Exception? No PostedFile? – Yuriy Galanter Nov 28 '13 at 03:16
  • It does not print anything to the `Reponse.Write`, it does not save file. – OneFineDay Nov 28 '13 at 03:16
  • Can u put a breakpoint at the `IF` and follow it step-by-step to see what is happening? – Yuriy Galanter Nov 28 '13 at 03:19
  • If I test it on localhost all works well with local folders. – OneFineDay Nov 28 '13 at 03:21
  • If its working fine on the local host and not working when hosted, then the most probable reason would be file permissions. Also right click the page to view source after submitting the file. You may not see the error written by response.write since the page conatins a lot of html and all. – Subin Jacob Nov 28 '13 at 04:28
  • I am now getting an error `Could not find a part of the path '\\{server blah}\{site}\{subfolder\{filename}'.` and this looks just like the path should be - what gives? – OneFineDay Nov 28 '13 at 06:07

3 Answers3

0

You have to set File Permissions if you are on Hosting provider. If you are using a shared hosting, then there would be options to do it. If you have access to IIS, then you can do it yourself.

Subin Jacob
  • 4,692
  • 10
  • 37
  • 69
0

Modify your third line like this

Dim _path As String = Path.Combine(Server.MapPath("{Relative url of your folder within your project}"), Server.HtmlEncode(fileUploader.PostedFile.FileName))

Relative URL should be something like this:

"~/folder/Folder2//"  

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Madhan Sekar
  • 194
  • 1
  • 10
  • `The SaveAs method is configured to require a rooted path` – OneFineDay Nov 28 '13 at 05:55
  • I am now getting an error `Could not find a part of the path '\\{server blah}\{site}\{subfolder\{filename}'.` and this looks just like the path should be - what gives? – OneFineDay Nov 28 '13 at 06:05
0

It finally worked, must be delay between publish and using the new files - cloud based. It is working as expected wit this path.

If fileUploader.HasFile Then
  Dim filePath = Path.GetFileName(fileUploader.PostedFile.FileName)
  Dim _path As String = Path.Combine(Directory.GetParent(Server.MapPath("~")).FullName, "subfolder", filePath)
  Try
    fileUploader.PostedFile.SaveAs(_path)
  Catch ex As Exception
    lb.Text = ex.Message
  End Try
Else
  lb.Text = "No File!"
End If

Thanks for the help everyone.

OneFineDay
  • 9,004
  • 3
  • 26
  • 37