Text Encoding comes out corrupted after downloading from FTP - c#

So i am making a program in c# with .net that uploads text files...
but everytime i upload a text file then download it on filezilla it comes out in chinese looking text..See here. im not sure if its because of encoding but if it helps heres my ftp code:
string ftpUsername = "#######";
string ftpPassword = "##########";
string localFilePath = path+ #"\" +FileName;
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
client.UploadFile("ftp://###########/Logs/Text.txt", "STOR", localFilePath);
File.Delete(path + #"\" + FileName);
}

The reason the text file was coming out corrupted was not the upload method but the download method in FileZilla...
When you download you need to set the transfer type too binary...
this gets rid of the issue..

Related

Uploading and downloading small files to a cloud drive in-game? (Unity3D)

I need any player's client to be able to upload and download files (they are text files that have replay data. I'm making a racing game and want players to be able to send and download replays.) while in-game. I'm trying to use Dropbox but I'm having problems. I'm open to other file hosting services but I don't really know how to send and get data from them. I keep making little changes here and there and they all give me different errors. Is there anything I'm doing blatantly wrong here? I'm expecting the main problem to be in the dropbox URL I'm using. Here's what my code looks like:
private void DownloadReplay()
{
string l_playerID = PlayFabManager.load_PlayfabId;
string l_levelID = PlayFabManager.load_levelID;
using (WebClient wc = new WebClient())
{
string path = Path.Combine(Application.persistentDataPath, $"{l_playerID}{l_levelID}.replay");
string url = ("https://www.dropbox.com/s/f786vep0z9yd11s/"+ $"{l_playerID}{l_levelID}");
wc.Credentials = new NetworkCredential("xx", "xx");
wc.DownloadFile((new System.Uri(url)), path);
}
}
private void UploadReplay()
{
string s_playerID = PlayFabManager.load_PlayfabId;
string s_levelID = PlayFabManager.load_levelID;
using (WebClient client = new WebClient())
{
string path = Path.Combine(Application.persistentDataPath, $"{s_playerID}{s_levelID}.replay");
string url = Path.Combine("https://www.dropbox.com/s/f786vep0z9yd11s/" + $"{s_playerID}{s_levelID}");
client.Credentials = new NetworkCredential("xx", "xx");
client.UploadFile(url, path);
}
}
Right now this one gives me an error 404, even though the file matching the string is in my dropbox.

C# Read file in ftp when this file start with dot "." (hidden Files) [duplicate]

This question already has answers here:
FtpWebRequest ListDirectory does not return hidden files
(2 answers)
Closed 2 years ago.
I need read file in ftp, but this file start with a "." (hidden Files), for exemple .teste.txt.
I tried read this file using this code:
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)WebRequest.Create("ftp://" + strFTP + ":" + strPorta + strDiretorio);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Credentials = new NetworkCredential(strUser, strPass);
response = (FtpWebResponse)reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
in this case I put " -al" in the end ftp url, using this code:
var reqFTP = (FtpWebRequest)WebRequest.Create("ftp://" + strFTP + ":" + strPorta + strDiretorio + " -al");
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Credentials = new NetworkCredential(strUser, strPass);
response = (FtpWebResponse)reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
Your problem may not be about the file containing a "." or not, by the looks of the code I bet the problem is the lack of a bar "/" between strPorta and strDiretorio.
You can also check if the concatenated string used to create the WebRequest has any kind of typo regarding special characters, if so, try to escape tehm with a "\" like when you use a new line in text "\n".
EDIT
After reading the comment It occurred me that you are in fact trying to list the so called hidden files. In UNIX based systems the "." before the filename is used to flag that file as hidden.
The problem here is that the Object that you are using to connect in the FTP does not have the ability to show/list hidden files as seem in this other thread:
FtpWebRequest ListDirectory does not return hidden files
I recommend that you use the solution proposed by the user Jothi Prakash Anandan and try other Libraries if you really need the hidden files.

Extra data when uploading txt file to FTP server using C#

Ok, so I am testing uploading files to an FTP server by attempting to upload a text file with the contents of "HELLO WORLD".
I am given a return of"
"upload file completed System.Net.WebClient error -> cancelled ->False".
The file seems to appear on the server but when I open it, the contents read:
--------------8d30e4d69803578
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
HELLO WORLD
--------------8d30e4d69803578
the code I am using is:
string ftpUserName = "ftpUserName";
string ftpPassword = "ftpPassword";
string ftpURL = "ftp://ftpServer.com/text.txt";
string path = "pathToFile/test.txt"
public static void Test()
{
System.Uri uri = new System.Uri(ftpURL);
FileInfo file = new FileInfo(path);
if (!file.Exists)
{
return;
}
using(WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(ftpUserName,ftpPassword);
wc.UploadFileCompleted += UploadFileCompleted;
wc.UploadFileAsync(uri,"STOR",path);
}
}
Any help would be appreciated
edit
I also just tried with a zip file and it is corrupt. Both the .txt and .zip are also far smaller once they reach the server, so I am assuming the upload has failed because of that error
edit 2
solved it using .net2.0's version of the FtpWebRequest
I have just verified - other than the async issue I mentioned, there's nothing wrong with your code.

Get all files in FTP directory

I am new to C# and want to get list of file names in ftp directory. But i read some answer on other topics WebRequestMethods is for the single request.
Here is my current code getting FTP object for a specific files.
I want all files ends with ".txt" and want regex like "*.txt". How can I get all file names
in FTP directory?
reqFTP = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri("ftp://95.0.181.84/bankToCompany/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(obj.ftpUserName,obj.ftpPassword);
System.Net.FtpWebResponse response = (System.Net.FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
FileStream outputStream = new FileStream(obj.collectionFileDownloadAddress + renameAddress, FileMode.Create);
StreamReader reader = new StreamReader(responseStream);
string contents = reader.ReadToEnd();
How to: List Directory Contents with FTP
FTP is an old and horrible protocol to work with even in .Net but MSDN has some documentaion about it here.
Check out the "How to: Download Files with FTP" and "How to: Upload Files with FTP" links to the left aswell if you need.

Download PDF file from Web location and Prompt user SaveAs box on client in web-Application ASP C#

I have link of a PDF file located on a website e.g. (https://www.mysite.com/file.pdf).
What I need is prompt the user (on Client side) the SaveAs box to choose the file location to save the file.
I tried the SaveFileDialog , but got know that it is only for Windows Forms. and my application is web based.
C# Code
var fileNumber = lblFileNumber.Text;
string fileDownloadLink = "http://www.mysite.com/" + fileNumber + ".pdf";
string fileName = fileNumber + ".pdf";
bool exist = false;
try
{
var request = (HttpWebRequest)System.Net.WebRequest.Create(fileDownloadLink);
using (var response = (HttpWebResponse)request.GetResponse())
{
exist = response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
}
if (exist)
{
var wClient = new WebClient();
wClient.DownloadFile(fileDownloadLink, fileName);
}
I wrote the above code, it does check if the file is exist on the file location, but
how to prompt user to choose the location to where he want to save file and save on his local hard drive?
It is not possible to do that. When the user wants to download a file, the browser will decide how to show it to the user.
Write a file from disk directly:
HttpContext.Current.Response.TransmitFile(#"C:\yourfile.pdf");
Or from another source, loaded as a byte array:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(bytesOfYourFile);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();

Categories