By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,968 Members | 1,852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,968 IT Pros & Developers. It's quick & easy.

Uploading files from one server to another over the web

P: n/a
I have an application that works with a SQL server database on
server No. 1 but I want to upload files for storage to Server No. 2
that is accessible only from the Internet. If the file storage was on
server No. 1, I would specify a physical file path (ChosenDirectory)
for storage of the uploaded file but in my case I need to specify a
virtual path like http://server2/folder1/.

I'm trying to use server.mappath to convert the virtual path to the
folder structure on server 2 but get an error - Invalid path for
MapPath 'http://server2/folder1/'. A virtual path is expected. I've
checked the virtual path by pasting into my browser and it is OK and
connects to the remote server No. 2. The sub works fine for physical
paths on the main server No. 1. Please help.

<%@ Page Language="vb" Explicit="true" Trace="false" Debug="true" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Net" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SQLClient" %>
<script runat="server">

Sub DoInsert(Source as Object, E as EventArgs) 'call to insert record

'first get the path for the file to be saved
Dim MyPath, MyName as string
Dim ChosenDirectory as string

ChosenDirectory= Server.MapPath ("http://server2/folder1/")

MyPath = Left(ChosenDirectory, Len(ChosenDirectory) - 1) '
Set the path by stripping off the final \
' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.

if MyName="" ' The folder is not there & to be created
MkDir(ChosenDirectory) 'Folder created
span2.InnerHtml="A Folder " & ChosenDirectory & " is created
at the Page_Load"
end if

'insert file metadata into sql server
Dim MyConnection As SqlConnection
Dim MyCommand As New SqlCommand
Dim strSQL As String
Dim intRowsAff As Integer
Dim strConnect as string
strConnect=ConfigurationSettings.AppSettings ("strConnect")
lblErrMsg.Text = ""
lblRecsAff.text = ""
MyConnection = New SqlConnection(strConnect) '
Dim UpFile as HttpPostedFile = UP_FILE.PostedFile
Dim sMessage as new StringBuilder()

If UpFile.ContentLength = nothing then

sMessage.Append("<b>* You must pick a file to upload</b>")
Exit Sub
End If

Dim strfilename As String
If Not (up_File.PostedFile Is Nothing) Then
Dim postedFile = up_File.PostedFile
strfilename = Path.GetFileName(postedFile.FileName)

Catch ex As Exception
lblRecsAff.text = "Failed uploading file"
End Try
End If

Dim FileLength as Integer = UpFile.ContentLength
Dim FileByteArray(FileLength) as Byte

MyConnection = New SqlConnection(strConnect) '

strSQL = "INSERT INTO tblAttachments (DocumentID, FileName,
ContentType, FileSize, Description, UploadDate,FilePath, FilePathNew )
" & _
" VALUES (@DocumentID, @FileName, @ContentType,
@FileSize, @Description, @UploadDate, @FilePath, @FilePathNew )"
MyCommand = New SqlCommand(strSQL, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("@DocumentID",, 4))
MyCommand.Parameters("@DocumentID").Value =
MyCommand.Parameters.Add(New SqlParameter("@FileName",
SqlDbType.NVarChar, 255))
MyCommand.Parameters("@FileName").Value = strfilename
MyCommand.Parameters.Add(New SqlParameter("@ContentType",
SqlDbType.NVarChar, 50))
MyCommand.Parameters("@ContentType").Value =
MyCommand.Parameters.Add(New SqlParameter("@FileSize",
MyCommand.Parameters("@FileSize").Value =
MyCommand.Parameters.Add(New SqlParameter("@Description",
SqlDbType.NVarChar, 255))
MyCommand.Parameters("@Description").Value =
MyCommand.Parameters.Add(New SqlParameter("@UploadDate",
MyCommand.Parameters("@UploadDate").Value = Now()
MyCommand.Parameters.Add(New SqlParameter("@FilePath",
SqlDbType.NVarChar, 150)) ' this stores the path from which the file

MyCommand.Parameters("@FilePath").Value =
MyCommand.Parameters.Add(New SqlParameter("@FilePathNew",
SqlDbType.NVarChar, 150)) ' this saves the path where the file will be
MyCommand.Parameters("@FilePathNew").Value = MyPath

intRowsAff = MyCommand.ExecuteNonQuery()
Catch ex As Exception
lblErrMsg.Text = ". A file with this name already exists.
Change file name and try again."
End Try
lblRecsAff.Text = intRowsAff & " file inserted"


txtDescription.visible = false
lblDescription.visible = false

'Upload file to folder structure
' Display properties of the uploaded file

FileName.InnerHtml = UP_FILE.PostedFile.FileName
FileContent.InnerHtml = UP_FILE.PostedFile.ContentType
FileSize.InnerHtml = UP_FILE.PostedFile.ContentLength
UploadDetails.visible = True

' recover only the file name from its fully qualified path at client

' Dim strFileName as string
strFileName = UP_FILE.PostedFile.FileName

Dim c as string = System.IO.Path.GetFileName(strFileName) ' only
the attached file name not its path

' Save uploaded file to server at the chosen directory

UP_FILE.PostedFile.SaveAs(ChosenDirectory + c)

catch Exp as exception
span1.InnerHtml = "An Error occured. Please check the
attached file"
UploadDetails.visible = false
End Try

span1.visible = true
span2.visible = true
Panel1.visible =false

btnSaveNew.enabled= false
AddNew.enabled = true

BindGrid() ' refresh the datagrid
End Sub

Mar 11 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.