I am trying this code to download a file from a Windows machine using C# against a Solaris machine and I receive error 550 - File unavailable.
string fileName = FileName();
string remoteUri = "xxxx";
var webClient = new WebClient();
webClient.Proxy = null;
webClient.Credentials = new NetworkCredential(Settings.Default.FtpUser, Settings.Default.FtpPassword);
webClient.BaseAddress = "ftp://"+Settings.Default.FtpHost;
webClient.DownloadFile(remoteUri, fileName);
I have validated that the URI works when using it in the address line of an Internet Explorer.
The URI looks like this
ftp://10.99.137.99/opt/scripts/overnight/test.txt
The actual location after the login on the Unix side is
/opt/scripts/overnight/test.txt
on the Unix side.
I am able to view the file after entering my user and password. What am I doing wrong? What other steps can I take? Is there an easy way to use more manual ftp?
Here's another interesting article with a different answer:
FtpWebRequest Download File
string remoteUri = "xxxx";
Have you posted the actual code? This is the name of the remote file. It needs to be ftp://10.99.137.99/opt/scripts/overnight/test.txt not xxxx
If it isn't the actual code, can you post the code you are really using?
Related
How to create a file from a local computer directly onto a web server?
I tried but got this error:
URI format not supported.
My code is:
string path = Server.MapPath("~/App_Data/");
string path = "http://www.domainname.com/App_Data/";
string jsondata = new JavaScriptSerializer().Serialize(enquirydet);
System.IO.File.WriteAllText(path + "enq.json", jsondata);
Any way how I can do that?
I have hosted my application on a server having IP 12.3.4.56 and my datastore reside on another server having IP 12.3.4.57 in same network.
I want to read XML file from my datastore server to application server.
When I fire "\\12.3.4.57\ABC\DEF\" in run prompt it opens the correct folder on both server. I have given everyone read/write shared access on folder also.
When I try to read file from my application server using below code it throws an error.
string XMLFilePath = "\\12.3.4.57\ABC\DEF\dir.xml";
XmlDocument DirDoc = new XmlDocument();
DirDoc.Load(XMLFilePath);
Error: The username or password is incorrect.
The same error occurred when I try to copy file from my datastore server to application server using below code.
string sourceFile = "\\12.3.4.57\ABD\DEF\Test123 (26).pdf";
string Folder = HttpContext.Current.Server.MapPath("~/SavedPDFs");
string destPDFFile = string.Concat(Folder, "Test123 (26).pdf");
System.IO.File.Copy(sourceFile, destPDFFile, true);
I believe you are forgetting to escape the backslash causing the "permission" problem.
Your code should look like this
string XMLFilePath = #"\\12.3.4.57\ABC\DEF\dir.xml"; // note the leading # sign
XmlDocument DirDoc = new XmlDocument();
DirDoc.Load(XMLFilePath);
string sourceFile = #"\\12.3.4.57\ABD\DEF\Test123 (26).pdf"; // note the leading # sign
string Folder = HttpContext.Current.Server.MapPath("~/SavedPDFs");
string destPDFFile = string.Concat(Folder, "Test123 (26).pdf"); // I assume that the Folder variable will have the frontslash at the end
System.IO.File.Copy(sourceFile, destPDFFile, true);
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.
" 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;
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
I have to upload a file via FTP to ftp://ftp.remoteServer.com
My root directory on remoteServer contains an "upload" and a "download" folder. I need to put my file in the "upload" directory. But on log in, the server automatically puts me in the "download" folder.
I tried doing this:
string serverTarget = "ftp://ftp.remoteServer.com/";
serverTarget += "../upload/myfile.txt";
Uri target = new Uri(serverTarget);
FTPWebRequest ftp = (FTPWebRequest)FtpWebRequest.Create(target);
using(Stream requestStream = ftp.GetRequestStream()) {
// Do upload here
}
This code fails with: (550) File unavailable (e.g., file not found, no access)
I debugged the code, and target.AbsoluteUri returns as ftp://ftp.remoteServer.com/upload instead of ftp://ftp.remoteServer.com/../upload (missing the ..)
If I put ftp://ftp.remoteServer.com/../upload in a browser, I can log in and verify this is the correct place where I want to put my file.
How can I get the FTPWebRequest to go to the correct place?
I believe you can encode the dots as %2E to keep the dots in your URI.
So something like:
string serverTarget = "ftp://ftp.remoteServer.com/%2E%2E/upload/myfile.txt";
Try this:
string serverTarget = "../upload/myfile.txt";
Uri uri = new Uri(serverTarget, UriKind.Relative);
Andy Evans' comment is correct.
Consider the URI: http://ftp.myserver.com/../. The .. means, "take me to the parent of this directory". But there is no parent! So when you derive the absolute URL, you're going to end up with http://ftp.myserver.com/ There is nothing else that the parser can do.
I think the problem is with the configuration of your FTP server. I assume that the directory structure looks something like:
ftproot
upload
download
It looks like the FTP service is automatically logging you to /ftproot/download. That is, the URI ftp.myserver.com gets mapped to /ftproot/download on the local file system. If that's the case, no amount of fiddling with the URI is going to get you anywhere. If the URI root is mapped to the download directory, there is no way you can, using the .. syntax, "go up one level and then down."
Are you able to upload using an FTP client such as Filezilla, or perhaps the Windows FTP command line tool? If so, what are the steps you take to do it? Can you make your code do the same thing?
I've written a simple code. I don't understand, what happens here. Please explain to me, where will I find a given upload file
WebClient client = new WebClient();
string uri = "http://localhost:8080/sample.txt";
client.Credentials = CredentialCache.DefaultCredentials;
string Filename = "F:\sample_test.txt/docx;
byte[] arrReturn = client.UploadFile(uri, "POST", Filename);
Well, to start with this clearly isn't your real code, as this line won't compile:
string Filename = "F:\sample_test.txt/docx/";
\s isn't a valid escape sequence However, assuming you had working code, what will happen is that your application would make an HTTP POST request with the contents of the file, to the given URI - in this case http://localhost:8080/sample.txt. It's entirely up to the server what it does with the request.
It could save the file on disk somewhere
It could save it to a database
It could post it to another web service
It could completely ignore the contents, and not save it anywhere
Nothing is guaranteed by the act of uploading the file - you're just making a request with some data.
Now if your URI really involves localhost, then it's uploading the file to the computer you're on - so you should be in control of what the web server listening on port 8080 is going to do with upload requests. Again, we can't tell you what it will do - it's up to the server.
It might be your filename:
string Filename = "F:\sample_test.txt/docx/";
Change the forward slashes to blackslashes, and give an actual filename as well, not just the path:
string Filename = #"F:\sample_test.txt\docx\";
Or
string Filename = "F:\\sample_test.txt\\docx\\";
Actually, the filename as is doesn't make much sense - I'm not sure how UploadFile would process it, even with the slashes going the correct way, as you appear to have a filename followed by a directory...? Shouldn't it actually be:
string Filename = #"F:\docx\sample_test.txt"?
It could be the URI - you're specifying the filename as well as the URI.
Did you look at the location specified in the URI? http://localhost:8080/?
If it's not there, then try wrapping your code in a try-catch block to see if any exceptions are thrown:
try
{
byte[] arrReturn = client.UploadFile(uri, "POST", Filename);
}
catch (Exception ex)
{
// do something here - in the debugger, you can inspect ex.Message to see the exception
}