Reputation: 60
I am using C# .net and I am trying to upload a file to my server. I am not using asp .net.
I followed This Question and it doesn't seem to work for me. On a sidenote, this question was written back in 2008.
I am using this code as shown in the question above:
File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\public_html\\new");
//tried this also
File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\new");
The error I get:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The network path was not found.
I also tried using my domain name and use the path like it shows up in the browser, without http://
because it complained about the format, saying its not accepted, like this: domain-name.com\\new
, and still got the same error as a result.
Any ideas what I could be doing wrong, or have a better solution on how to upload a file to a server ? You can see here that the http://198.57.247.152/~shiro/new/ path exists.
Upvotes: 0
Views: 2790
Reputation: 10622
You can upload file using code from Peter Luu's answer. But first you should have access to it.
For uploading a file to a remote server, you should be a user of it and should have a password. It is not the password you use for logging in to your Hostgator control panel. After logging into your Hostgator account, there will be an option like FTP in it, where you can setup FTP user accounts. There you can assign a user name and password and that have to be applied to the code for uploading..
And for checking whether the UserName and Password works, Open MyComputer, and in the addressbar type the FTP path (which will be something starting "ftp://"). There pops up a Dialog asking for UserName and Password (If the path is valid). If you enter into, See and copy files into the explorer window from there, then the uploading code will work
If you want to get the applicable path, Open windows explorer and type ftp://your_domain_name. Apply UserName and Password, browse through the path you need, copy it from the address bar and add it to the code.
Upvotes: 0
Reputation: 446
The path \\198.57.247.152\~shiro\new is what Microsoft calls Uniform Naming Convention (UNC). This type of resource is only available on networks with NetBIOS enabled; essentially local network. The problem is that File.Copy
only works if you have access to it in your network - since this is a remote server, it won't be able to find that server, leading to the The network path was not found
exception.
http://198.57.247.152/~shiro/new/ follows the syntax of <scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]
which is call Uniform resource locator (URL). Hypertext Transfer Protocol (http) resource is typical accessed by a browsers.
You can resolve this by instead using FTP to upload your file to the server:
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://ftpserver.com/targetpath", "localfilepath");
}
A valid target path would be something like: ftp://localhost/samplefile.txt and the local filepath should be the fully qualified filepath.
Upvotes: 3