Uploading a .txt file to an FTP server - c#

I am trying to upload a .txt file to an ftp server using this example http://msdn.microsoft.com/en-us/library/ms229715.aspx
I get a "The requested URI is invalid for this FTP command" error .
When i change request.method from WebRequestMethods.Ftp.UploadFile to WebRequestMethods.Ftp.UploadFileWithUniqueName it works..
But this way a .tmp file is created with a random name. Any suggestions on how to upload the txt ?

I pasted the code from the link, and got the same problem.
Since I created the ftp server, the problem was that the user didn't have delete permissions, so the file couldn't be overridden nor appended.
Once I've set the permissions to do that, the code works, and I can see the file uploaded. (also tried appending, and it works as well).
Can you make sure you have the permissions to write to the FTP?
If you can do this once only, you probably have read/write permissions, but no delete, so it fails. It'll be easy to test, just give a new name to the file and see what happens
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp_address/new_file_name_here");

Related

FTP file To Server

I am currently using FTPwebRequest to move a local file over to a server. I am able to FTP to the root directory at ftp://ftp.xxxx.com. But, whenever I try to FTP the file to a folder within that directory like: ftp://ftp.xxxx.com/firstfolder nothing happens. I don't get any hard halts in the code and I also setup a FTPwebResponse stating that the transfer is complete.
string dest = "ftp://username:password#ftp.xxxx.com/firstfolder/" + fileName;
ftp = (FtpWebRequest)FtpWebRequest.Create(dest);
I have also tried using %2f to mimic the CD command.
Here are a few links I have been looking at with no luck:
https://blogs.msdn.microsoft.com/mariya/2006/03/06/changing-to-the-root-directory-with-ftpwebrequest
https://social.msdn.microsoft.com/Forums/en-US/91e2bed0-9e5e-4503-9e66-d224086e43a8/change-directory-with-ftpwebrequest
https://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest(v=vs.110).aspx
In IE the file did not appear in the servers directory. I used google chrome and I was able to view the file successfully. It was uploaded the hole time.

Upload file to server .Net

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

How to save the XML files in Computer?

In my project my app receives an XML from another program. I want to save this XML file in the folder in my PC. But if there is another XML file that will come, the XML will be added in the folder not overwrite the existing XML. How can I achieve it?
I am using this code right now but I don't know why I got an access denied error.
System.IO.File.WriteAllText(#"C:\XML", abc);
First of all, the error that you receive is because of Windows UAC which denies users without Administrator privileges to write on the C:\ disk. Create a folder in which you have access, for example "c:\temp\" and write your files in there.
Also, you specify a file and not folder.
And if you don't want to overwrite the file, just make sure to generate a unique filename (a guid perhaps).

How do I get the filename of the .msg file when I use specifiedPickupDirectory for my .Net SMTP

I'm trying to keep track of the email created after I "send" it using SmtpClient.Send.
I have it configured to write to a directory by configuring my app.config to use specifiedPickupDirectory.
What I'd like to gain access to is the name of the file that was used, so that I can periodically check and make sure that my mail server has retrieved it and sent it along.
Any suggestions?
Perhaps try initially creating the file in a temporary directory. Make sure that it is the only file in the directory. Use Directory.GetFiles to find the file name and save it to a variable. Then move the file to the real directory.

Can't delete a file from a client after uploading it to the server

I'm using asp:FileUpload control to upload a file to the server. Nothing fancy there, just
FileUploadId.Save();
File gets uploaded successfully, and everything is fine until I try to delete that file on the CLIENT. I get a good-old "File is being used by another person or program" message.
How do I make sure that file is not being accessed on the client after it's been uploaded?
EDIT
deleting the file has nothing to do with the application. i'm just trying to delete the file manually since i don't need it any more.
EDIT2
closing the browser fixed the problem ... any ideas?
Since the problem happens both in IE and FF: could it be that the file is locked by some AntiVirus software?
The issue might be the file can be locked by the aspnet process even after uploading. Once you close the IE, the aspnet process release the file
How are you trying to delete the file at the client? Unless you're hosting in WebBrowser, or using something like an ActiveX control, you only have javascript at the client - and that doesn't provide random file access.
So: what is the full setup here?
A thought. It may not be the file upload that is causing the problem. As the surrounding code isn't posted it's difficult to tell, but, for example, do you have a Zip manager object of some kind that you're not disposing of?

Categories

Resources