Martin Smellworse
Martin Smellworse

Reputation: 1752

Accessing and uploading a file from a path

I have a page where users can upload files. I don't save the files - I use a stream and upload them to a 3rd party server. I do store the path in a database.

I now need to upload those files to a different server. So, I have a path in a database like:

J:\Projects\Commercial\somedoc.docx

and I now need to allow users to select and upload the file at that location to another server. I can't use the path in a control - as you cannot set the value of such a control.

I am going to display a list of the file paths on a .aspx page for a user to select which files they need to upload (again, but to a different server).

How can I upload the file when all I have is a string which is the path?

Normally I would have:

HttpFileCollection hfc = Request.Files;
HttpPostedFile hpf = hfc[0];
using (Stream fx = hpf.InputStream)
{
//send the stream to a remote server here
}

but I don't have the posted file to work with, all I have is a path.

Upvotes: 0

Views: 233

Answers (2)

freefaller
freefaller

Reputation: 19953

As per your comment...

Yes, they have uploaded a load of different files to Server A and now, a few weeks later, need to see a list of the files they uploaded to Server A and upload them to Server B. (It is not possible for me to copy from Server A to Server B as I don't have access to them

As you have already stated, you cannot pre-select a file on a users computer - which is completely correct, as otherwise it would be a massive security hole.

If you have no ability to directly transfer the files from the Server A to Server B, then you simply have no other choice than to request the user to select those files again.

Maybe a way forward would be to list all the files they have uploaded, and provide individual upload controls for them to populate.

Be aware that there is normally a limit to the maximum "request" size that IIS will accept (which can easily be reached if uploading multiple files in a single go). This limit can be raised through configuration, but a higher value increases the resource loading on the server

Upvotes: 0

pravprab
pravprab

Reputation: 2293

" need to upload (again, but to a different server) ". means you have to copy from one server to another. So i think you have to use FTP copy.

The below code may help you. The code is not tested. Please try

string CompleteDPath = "";
CompleteDPath = "ftp://1234.1234.12.13/Projects/Commercial";


string UName = "";
string PWD = "";
UName = "Administrator";
PWD = "12345";


WebRequest reqObj = WebRequest.Create(CompleteDPath + "somedoc.docx");
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
reqObj.Credentials = new NetworkCredential(UName, PWD);
FileStream streamObj = System.IO.File.OpenRead(physical path + "somedoc.docx");
byte[] buffer = new byte[streamObj.Length + 1];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);
reqObj = null;

Upvotes: 1

Related Questions