FTP file To Server - c#

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.

Related

Prevent incomplete file transfer over FTP using WinSCP .NET Assembly

I am simply trying to transfer text files from one FTP server to another using a windows service. I download the required files from source FTP server and save it locally on my system and then upload the saved file to the destination server. For downloading and uploading files I am using WinSCP .Net Assembly. Here is my code that I am using to transfer files to the destination server:
WinSCP.SessionOptions sessionOptions = new WinSCP.SessionOptions();
sessionOptions.Protocol = WinSCP.Protocol.Ftp;
sessionOptions.UserName = "myUsername";
sessionOptions.Password = "myPassword"
sessionOptions.PortNumber = 21;
sessionOptions.HostName = serverIPAddress;
session.Open(sessionOptions);
WinSCP.TransferOptions transferOptions = new WinSCP.TransferOptions();
transferOptions.TransferMode = WinSCP.TransferMode.Binary;
WinSCP.TransferOperationResult transferResult;
transferResult = session.PutFiles(PathToLocalFile + filename, destinationFilePath, false, transferOptions);
transferResult.Check();
It works fine and uploads file to the server, but in case a connectivity issue occurs while transferring the file, an incomplete chunk of required file is transferred to the destination server.
I have searched the WinSCP official documentation but I couldn't find anything related to this.
Is there any way to ensure that only complete files gets transferred to the destination otherwise (in case an error occurs during transfer), the transferred chunk of file gets deleted automatically? (Without manually deleting the incomplete file)
There no way to make this automatic.
You have to code it. Just check, if the transfer failed, reconnect (if needed), and delete the partially uploaded file.
Though as already mentioned in comments, if the transfer fails, because of problems with connection, you may not be able to reconnect to delete the file.
There's no magic solution. The server should be able to deal with partial files in the first place.
See also:
How to detect that a file is being uploaded over FTP (while seemingly different topic, detecting if file is being uploaded is basically the same thing, as detecting if file has not been uploaded completely)
File upload with WinSCP .NET/COM with temporary filenames

WinSCP Move file after upload with C#

I am creating an SFTP upload program. It is working great, it connects to the remote SFTP server and uploads the files as intended. The issue I am having it I want the files once uploaded moved to a new directory on the local server. I have searched WinSCP site and did google searches, but the code I am up with it not working. Here is what I have:
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
session.MoveFile(transfer.FileName, Local_Processed);
}
In the log it states that it is moving the files but the files remain in the original folder and nothing appears in the processed folder.
The Session.MoveFile is for moving a remote file to another remote directory or for renaming a remote file. It's not for moving a remote file to a local directory.
To move a remote file to a local directory, use the remove parameter of the Session.GetFiles.
Though for me it looks like you actually want to move an original local file (that was uploaded) to another local directory. So it has actually nothing to do with WinSCP.
To move a local file, use the File.Move:
File.Move(transfer.FileName, destinationPath);
Here is what ended up with after Martin Prikryl posted. I ended up having to add a second foreach after my first one used to just move the files. I also found that the *.* in my original original directory call had to be left out as this was also causing issues.
I ended up creating a second variable in my app.config file. It had the exact same path as the original directory variable except it didn't have the *.* for file name.
foreach (var file in Directory.GetFiles(OrgPath))
{
File.Move(file, Path.Combine(Processed, Path.GetFileName(file)));
}

Uploading a .txt file to an FTP server

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");

Why am I recieving this error: "Could not find a part of the file '...' "?

What I am trying to accomplish is to upload some files from one domain on my shared hosting to another domain on the same hosting where the files will be displayed. When I debug the application, the process gets to the SaveAs() method and then throws the exception,"Could not find a part of the path ..." .
I have followed these instructions on finding my site's folder's absolute path and I have implemented this path in my code, using the same method I've been using for a good part of my file uploading, and I have never ran into any problems. My read/write permissions are allowed for the folder that I'm attempting to save these files in.
I'm wondering "Is it because I'm trying to upload the file to a different directory?". If so, is there a better way to accomplish this?
var fileName = Path.GetFileName(file.FileName);
var path = #"D:\Hosting\someNumbers\html\SiteFile\SiteImages\" + fileName;
file.SaveAs(path);
myObject.FilePath1 = path;
Any help will be highly appreciated.
As it turns out, my error was more or less a security issue with GoDaddy's hosting. GoDaddy sees this type of action as a "third party FTP request", which is not allowed. In conclusion, GoDaddy does not allow a user to upload a file on one site, and then FTP that file to another site on the same hosting plan.

C# filepath from fileuploader fails to be read by streamreader asp.net

Running into an issue with a streamwriter
I have a web page that has a FileUploader in a loginview to access it I am using
var fileuploader = (FileUpload)LoginView.FindControl("FileUploader");
string filepath = System.IO.Path.GetFullPath(fileuploader.FileName.ToString());
Then I pass that data in to my streamreader which is (in a different class)
using (StreamReader reader = File.OpenText(filename))
The filepath that it is passing in is C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\sample.txt
Where I am selecting it from C:\fasta\sample.txt
I have seen some posts about this, but not concerning asp.net applications. Thanks!
The FileUpload class allows a client to upload/select a file. So all you can get would be a clientPath. Such a path would of no need to you! So what you can do is using the fileUpload.FileName property and combine it with some other path.
var fileuploader = (FileUpload)LoginView.FindControl("FileUploader");
string filepath = Path.Combine(Server.MapPath("."), fileuploader.FileName);
fileuploader.SaveAs(filePath); // will save the selected file on your server
Server.MapPath(..) maps a virtual path (at the server) to a real path at your server. Calling it with "." means this call will return your webApplication root directory. Anyway you should be very careful when uploading files where to save those! In worst case someone uploads a potential risky file (eg .aspx extension) and can execute code on your server!
Furthermore there is no need and no way to access a file at the client from the server directly. You may only get those items within the HTTP-Request. So the selectedFile is already in the Request and you can save it directly to your server harddisk!
I would suggest using the SaveAs method on the FileUpload control (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx).
Remember, the file is actually uploaded to you, and from a website, you'll never really want to care about the location it was at on the client machine (it just so happens the server and client are the same machine for you here). If you uploaded the file to the site from a different machine, the web server would never have access to the resource at the client's path. The FileUpload control has the file for you, so just take the file, place where you want, and then you can access it and do whatever you want with it.
Cliffs: even if you get the client path to the resource, your server won't be able to do anything with it since it is on another machine which your server doesn't have access to.
If you want to get the file uploaded:
.aspx
...
<asp:FileUpLoad id="FileUpLoad1" runat="server" />
...
code-behind
...
if (FileUpLoad1.HasFile)
{
FileUpLoad1.SaveAs(#"C:\temp\" + FileUpLoad1.FileName);
}
else
{
// No file uploaded
}
...
If you want the file path, the one that client had set in the browser control:
You can't. For security purposes, the browser will never post the full file's path.

Categories

Resources