473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 11 '06 #1
0 1874

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

Similar topics

5
7836
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 files via FTP. I'm using ftplib to do it, and for each file I'm using transfercmd("STOR " + myfile) to get the socket, then uploading 4096 bytes at a time and providing status updates via a GUI interface. Finally, I close the socket, set it to...
13
4317
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 that this is suppossed to end up as a component for others to use, and therefore I do NOT have access to their global.cs::Session_End() how do I cleanup files that were uploaded -- but obviously left stranded when the users aborted/gave up writting...
2
1434
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" runat="server"> I looked at some of the upload examples and they all seem to have the uploading on a page by itself where the form is set to something like: <form Method="Post" EncType="Multipart/Form-Data" RunAt="Server">
2
3953
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 uploaded. Here's a snippet of my handler code: // *** BEGIN HANDLER CODE *** // public class AutoUpload : IHttpHandler { public void ProcessRequest(HttpContext context) {
5
2726
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 more documents to share/use during a meeting presentation. What would be the most efficient way to approach this? This is the logic I'm currently considering: Page 1: Meeting Information input with link to a document upload page (this page...
221
367597
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 needs to store entire files, the preferred method is to save the file onto the server’s file-system, and store the physical location of the file in your database. This is generally considered to be the easiest and fastest way to store files. ...
1
726
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 uploading the files on the server and experienced the same issues as I get on any other computer. Thanks in advance for your help. Dan
3
5181
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
2767
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 asp101.com. http://www.asp101.com/articles/jacob/scriptupload.asp My three files are: Syllabus.htm <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9266
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6054
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.