upload a file to a different server

  • Thread starter Thread starter carlos.zamora.quesada
  • Start date Start date
C

carlos.zamora.quesada

Hey guys I'm trying to upload a file from one server to another I have
been trying several ways but I'm still not getting the results I want.

Basically I have the HtmlInputFile component and I have read the file
using:
HtmlInputFile fileInput;
byte[] buffer = new byte[fileInput.PostedFile.ContentLength];
fileInput.PostedFile.InputStream.Read(buffer, 0, buffer.Length);

After that I want to upload the file to a different server using:
string targetFileName = "http://otherwebsite.net/tmp/new.file";
System.Net.WebClient client = new System.Net.WebClient();
client.UploadData(targetFileName, buffer);

But this doesn't create the file on the other server. That folder on
the other server has full write permissions
I try also adding credentials to the webclient component.
client.Credentials = new System.Net.NetworkCredential("user", "pass");

I have been reading a lot of things about it but I'm still can't
find the way to create such file.

Already try:
WebRequest req = WebRequest.Create(targetFileName);
req.Method = "POST";
req.ContentLength = buffer.Length;
req.ContentType = fileInput.PostedFile.ContentType;
Stream str = req.GetRequestStream();
str.Write(buffer,0,buffer.Length);
str.Flush();
str.Close();

But nothing seems to be working.

Is there anyone that can help me out how to do it?
 
Hello Carlos

Have you set the html page to include "enctype="multipart/form-data",
e.g.
<form enctype="multipart/form-data" runat="server">

Let me know and we can try and figure it out.

Regards,
Simon
 
If you want to use this method, there is no difference with the usual upload
with htmlInputFile. That is you POST the data to an *existing* page. This is
this server side page that will finally save the file on the server.
 
Of course there is a difference because I want to upload the file on a
different server.
This is because I can't upload anything to the actual server so I need
to use another server to upload the content.
JIC, both servers are in different domains and OS.

Regards,
 
I already try with your suggestion but it doesn't work.
I have uploaded files to the same server just doing something like
this:

HtmlInputFile fileInput;
fileInput.PostedFile.SaveAs(Server.MapPath("my_path") + "\\" +
filename);

But the issue now is that this method can't be used with a URI like the
one I'm trying to use now.
Do you have any other clue?
 
Which URI ? Server.MapPath returns the full path name to a file. I would
write down the whole value (Server.MapPath("my_path") + "\\" + filename) to
see if it looks correct.

For now :
- either filename is not correctly initialized
- you perhaps meant Server.MapPath(my_path) instead of
Server.MapPath("my_path") ? Is this a variable in which you have the path or
do you want to save in a (sub) directory named "my_path" ?
 
That's exactly what I DON'T wanna do.
I need to upload the file to a different server not to the same box.
That means that this Server.MapPath will not work because it can't work
with URI like
"http://newserver/folder/filename.ext"

I have been trying to explain that before but it seems that I couldn't.
Thanks for your answer.
 
Thanks guys for all your help!
I solve the issue using a different approach.
Just in case you'll like to know, I create a little HTTPRequest
application that stands on the other web server and creates the file on
it.
So I basically have a page that generates a request to another server
and the other server takes the data and creates the file.

Here is the code to create the request on C#:

string reqPage = @"http://otherWebserver/index.php";
byte[] buffer = new byte[fileInput.PostedFile.ContentLength];
fileInput.PostedFile.InputStream.Read(buffer,
0,fileInput.PostedFile.ContentLength);
WebRequest req = WebRequest.Create(reqPage);
req.Method = "POST";
req.ContentLength = buffer.Length;
req.ContentType = @"application/octet-stream";
Stream str = req.GetRequestStream();
str.Write(buffer,0,buffer.Length);
str.Close();
str.Flush();


And with this page "http://otherWebserver/index.php" I take that data
and create the file on the other server.

Thanks for all your comments!
Regards!
 
Ok, I thought this page was already on your target server. So move the page
to the server on which you want to save the file. You'll be then able to
post to this server (from the other one)...

(of course the other option would be to use an UNC path but I suppose that
you have the problem because the other server can only be reached throught
http).
 
Back
Top