473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Uploading files from one server to another over the web

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 (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.

Thanks,
Lou
<%@ 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) '
MyConnection.Open
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
else
End If

Dim strfilename As String
If Not (up_File.PostedFile Is Nothing) Then
Try
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) '
MyConnection.Open

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",
SqlDbType.int, 4))
MyCommand.Parameters("@DocumentID").Value =
Session("DocumentID")
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 =
UpFile.ContentType
MyCommand.Parameters.Add(New SqlParameter("@FileSize",
SqlDbType.Int))
MyCommand.Parameters("@FileSize").Value =
UpFile.ContentLength
MyCommand.Parameters.Add(New SqlParameter("@Description",
SqlDbType.NVarChar, 255))
MyCommand.Parameters("@Description").Value =
Server.HtmlEncode(txtDescription.text)
MyCommand.Parameters.Add(New SqlParameter("@UploadDate",
SqlDbType.DateTime))
MyCommand.Parameters("@UploadDate").Value = Now()
MyCommand.Parameters.Add(New SqlParameter("@FilePath",
SqlDbType.NVarChar, 150)) ' this stores the path from which the file
came

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

Try
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"

MyConnection.Close

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
Try

UP_FILE.PostedFile.SaveAs(ChosenDirectory + c)

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

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

btnSaveNew.enabled= false
Datagrid1.enabled=true
AddNew.enabled = true

BindGrid() ' refresh the datagrid
End Sub

Mar 11 '06 #1
0 1842

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Kevin Ollivier | last post by:
Hi all, I've come across a problem that has me stumped, and I thought I'd send a message to the gurus to see if this makes sense to anyone else. =) Basically, I'm trying to upload a series of...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
2
by: tshad | last post by:
I have a page that I am using for multiple uses that includes reposting of the page as well as going to another page where the "form" tag would be something like: <form id="something"...
2
by: FusionGuy | last post by:
I've created a file uploading handler, implemented as an httpHandler. Each time I attempt to upload a file, or files, my HttpContext.Request.Files property never contains the files that were...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
by: =?Utf-8?B?RGFu?= | last post by:
MS won't seem to let me reply to my old post, so I created a new one. The error occurs in all browsers. It's definitely a server issue, not client. The server is not proxied in any way. I tried...
3
by: muziburrehaman | last post by:
i am looking for code in php to upload the 1 gb files. any one can please help me by sending the code....
0
by: LoriFranklin | last post by:
I'm a bit of a newbie here. I've learned a lot from reading the posts you all have here. I need some help uploading files using an asp form. I am using some code that I found from Jacob at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.