I have an asp.net 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 (ChosenDirector y)
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.
Thanks,
Lou
<%@ Page Language="vb" Explicit="true" Trace="false" Debug="true" %>
<%@ import Namespace="Syst em" %>
<%@ import Namespace="Syst em.IO" %>
<%@ import Namespace="Syst em.Net" %>
<%@ import Namespace="Syst em.Data" %>
<%@ import Namespace="Syst em.Data.SQLClie nt" %>
<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(ChosenDire ctory, Len(ChosenDirec tory) - 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(ChosenDir ectory) '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=Conf igurationSettin gs.AppSettings ("strConnect ")
lblErrMsg.Text = ""
lblRecsAff.text = ""
MyConnection = New SqlConnection(s trConnect) '
MyConnection.Op en
Dim UpFile as HttpPostedFile = UP_FILE.PostedF ile
Dim sMessage as new StringBuilder()
If UpFile.ContentL ength = nothing then
sMessage.Append ("<b>* You must pick a file to upload</b>")
Exit Sub
else
End If
Dim strfilename As String
If Not (up_File.Posted File Is Nothing) Then
Try
Dim postedFile = up_File.PostedF ile
strfilename = Path.GetFileNam e(postedFile.Fi leName)
Catch ex As Exception
lblRecsAff.text = "Failed uploading file"
End Try
End If
Dim FileLength as Integer = UpFile.ContentL ength
Dim FileByteArray(F ileLength) as Byte
MyConnection = New SqlConnection(s trConnect) '
MyConnection.Op en
strSQL = "INSERT INTO tblAttachments (DocumentID, FileName,
ContentType, FileSize, Description, UploadDate,File Path, FilePathNew )
" & _
" VALUES (@DocumentID, @FileName, @ContentType,
@FileSize, @Description, @UploadDate, @FilePath, @FilePathNew )"
MyCommand = New SqlCommand(strS QL, MyConnection)
MyCommand.Param eters.Add(New SqlParameter("@ DocumentID",
SqlDbType.int, 4))
MyCommand.Param eters("@Documen tID").Value =
Session("Docume ntID")
MyCommand.Param eters.Add(New SqlParameter("@ FileName",
SqlDbType.NVarC har, 255))
MyCommand.Param eters("@FileNam e").Value = strfilename
MyCommand.Param eters.Add(New SqlParameter("@ ContentType",
SqlDbType.NVarC har, 50))
MyCommand.Param eters("@Content Type").Value =
UpFile.ContentT ype
MyCommand.Param eters.Add(New SqlParameter("@ FileSize",
SqlDbType.Int))
MyCommand.Param eters("@FileSiz e").Value =
UpFile.ContentL ength
MyCommand.Param eters.Add(New SqlParameter("@ Description",
SqlDbType.NVarC har, 255))
MyCommand.Param eters("@Descrip tion").Value =
Server.HtmlEnco de(txtDescripti on.text)
MyCommand.Param eters.Add(New SqlParameter("@ UploadDate",
SqlDbType.DateT ime))
MyCommand.Param eters("@UploadD ate").Value = Now()
MyCommand.Param eters.Add(New SqlParameter("@ FilePath",
SqlDbType.NVarC har, 150)) ' this stores the path from which the file
came
MyCommand.Param eters("@FilePat h").Value =
Server.HtmlEnco de(UP_FILE.valu e)
MyCommand.Param eters.Add(New SqlParameter("@ FilePathNew",
SqlDbType.NVarC har, 150)) ' this saves the path where the file will be
stored
MyCommand.Param eters("@FilePat hNew").Value = MyPath
MyCommand.dispo se
Try
intRowsAff = MyCommand.Execu teNonQuery()
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"
MyConnection.Cl ose
txtDescription. visible = false
lblDescription. visible = false
'Upload file to folder structure
' Display properties of the uploaded file
FileName.InnerH tml = UP_FILE.PostedF ile.FileName
FileContent.Inn erHtml = UP_FILE.PostedF ile.ContentType
FileSize.InnerH tml = UP_FILE.PostedF ile.ContentLeng th
UploadDetails.v isible = True
' recover only the file name from its fully qualified path at client
' Dim strFileName as string
strFileName = UP_FILE.PostedF ile.FileName
Dim c as string = System.IO.Path. GetFileName(str FileName) ' only
the attached file name not its path
' Save uploaded file to server at the chosen directory
Try
UP_FILE.PostedF ile.SaveAs(Chos enDirectory + c)
catch Exp as exception
span1.InnerHtml = "An Error occured. Please check the
attached file"
UploadDetails.v isible = false
span2.visible=f alse
End Try
span1.visible = true
span2.visible = true
Panel1.visible =false
btnSaveNew.enab led= false
Datagrid1.enabl ed=true
AddNew.enabled = true
BindGrid() ' refresh the datagrid
End Sub