I am using FtpWebRequest to download files, but in all text files all \r\n are removed when downloaded.
What am I doing wrong?
Uri u = new Uri(msg);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(u);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = credential;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Get a reponse
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream localfileStream = new FileStream(destination,
FileMode.Create, FileAccess.Write);
//create the file
byte[] buffer = new byte[1024];
int bytesRead = responseStream.Read(buffer, 0, 1024);
while (bytesRead != 0)
{
localfileStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, 1024);
}
localfileStream.Close();
response.Close();
responseStream.Close();.
Your code is correct. There is something else you are not telling that causing the problem. My guess would be
a) File on the server does not have \r\n
b) The way how you check that it does not have them on the client is wrong. Either you are checking a wrong file, or it had gone additional transformation or something else.
I've had a similar problem to this. The code I used is almost identical to yours. I found that the code for me was actually working correctly, but the file on the server only contained the the "LF" character at the end of the line, not the "CR/LF" combo. Most text editors ignore this, and display the text as one continuous line.
Related
I am using the following code to upload a image to my FTP server but I have a problem. If I use the code to check the dimension (height and width) of the image before upload then there is .png file created in the FTP server but it's empty (or invalid format) and if I remove the code to check the dimension, then the image is uploaded correctly. Does any have any idea on this?
public ActionResult UploadFile(int type, HttpPostedFileBase imagefile)
{
//check image height and width
using (System.Drawing.Image image = System.Drawing.Image.FromStream(imagefile.InputStream, true, true))
{
if (image.Width > 160 || image.Height > 160)
{
//do something here
}
}//end check image height and width
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftpPath" + "/" + imagefile.FileName);
request.Credentials = new NetworkCredential("ftpUserName", "ftpPassword");
request.Method = WebRequestMethods.Ftp.UploadFile;
var sourceStream = imagefile.InputStream;
Stream requestStream = request.GetRequestStream();
request.ContentLength = sourceStream.Length;
int BUFFER_SIZE = imagefile.ContentLength;
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
do
{
requestStream.Write(buffer, 0, bytesRead);
bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
} while (bytesRead > 0);
sourceStream.Close();
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
As mentioned, you are reading up all the stream when you load it into your Image. However, I don't think you can reset the position (i.e. "Seek") on a NetworkStream (your InputStream). Once you read it, it is gone.
One thing you can do though is to create a MemoryStream and use Stream.CopyTo to copy the contents into that. Then, you can do anything you like with it, including resetting the position to 0 to "read it a second time".
//example of resetting a stream named "s"
s.Position = 0;
Thanks guys for the reply they were very helpful but I fixed this issue by making slight changes on my code as below, I checked the image dimension after reading the input Stream. May be someone find this helpful.
public string ftpUpload(HttpPostedFileBase imagefile, string filename)
{
var sourceStream = imagefile.InputStream;
int BUFFER_SIZE = imagefile.ContentLength;
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
if (!CheckLogoDimension(sourceStream))
{
sourceStream.Close();
return "error";
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpRootPath + "/" + filename);
request.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
Stream requestStream = request.GetRequestStream();
request.ContentLength = sourceStream.Length;
do
{
requestStream.Write(buffer, 0, bytesRead);
bytesRead = sourceStream.Read(buffer, 0, BUFFER_SIZE);
} while (bytesRead > 0);
sourceStream.Close();
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
return "Success";
}
I am programming in Microsoft Visual C# 2010 Express.
I have a text file in a folder on my web server containing one character: '0'.
When I start my C# Application I want to read the number from my text file, increase it by 1, and then save the new number instead.
I've browsed the web but I can't find a good answer. All I get is questions and answers about writing/reading from local text files.
So basically, I want to write some text to a text file which is not on my computer but here:
http://mywebsite.xxx/something/something/myfile.txt
Is this possible?
You may have to adjust the path directory, but this works:
string path = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) + "\\something\\myfile.txt";
string previousNumber = System.IO.File.ReadAllText(path);
int newNumber;
if (int.TryParse(previousNumber, out newNumber))
{
newNumber++;
using (FileStream fs = File.Create(path, 1024))
{
Byte[] info = new UTF8Encoding(true).GetBytes(newNumber.ToString());
fs.Write(info, 0, info.Length);
}
}
I found a working solution, using File Transport Protocol as Barta Tamás mentioned.
However, I learned from Michael Todd that this is not safe, so I will not use it in my own application, but maybe it can be helpful to someone else.
I found information about uploading files using FTP here: http://msdn.microsoft.com/en-us/library/ms229715.aspx
void CheckNumberOfUses()
{
// Get the objects used to communicate with the server.
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://mywebsite.xx/public_html/something1/something2/myfile.txt");
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://mywebsite.xx/something1/something2/myfile.txt");
StringBuilder sb = new StringBuilder();
byte[] buf = new byte[8192];
HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
int numberOfUses = int.Parse(tempString) + 1;
sb.Append(numberOfUses);
}
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
ftpRequest.Credentials = new NetworkCredential("login", "password");
// Copy the contents of the file to the request stream.
byte[] fileContents = Encoding.UTF8.GetBytes(sb.ToString());
ftpRequest.ContentLength = fileContents.Length;
Stream requestStream = ftpRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpResponse.Close();
}
Read in the comments of the question how this can be done better, not using FTP. My solution is not suggested if you have important files on your server.
My problem is that when I view the uploaded file on my ftp server, there is no contents on it, it is empty. Why is this happening am I missing something in my code?
if (File.Exists("main.xml"))
{
using (Stream stream = new MemoryStream())
{
stream.Position = 0;
stream.Seek(0, SeekOrigin.Begin);
string uri = String.Format("ftp://{0}/{1}/{2}", "host", "wwwroot", "main.xml");
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
request.Credentials = new NetworkCredential("user", "pass");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.KeepAlive = false;
request.UseBinary = true;
request.UsePassive = true;
request.ContentLength = stream.Length;
request.EnableSsl = false;
int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int contentLength;
Stream ftpStream = request.GetRequestStream();
contentLength = stream.Read(buffer, 0, bufferLength);
while (contentLength != 0)
{
ftpStream.Write(buffer, 0, contentLength);
contentLength = stream.Read(buffer, 0, bufferLength);
}
ftpStream.Flush();
ftpStream.Close();
}
}
else
{
MessageBox.Show("No file found.");
}
But when I try to insert this line of code above
MessageBox.Show(XDocument.Load("main.xml").ToString());
It has an output,
But when I try setting a breakpoint , when It reach while (contentLength != 0) it does not enter to it
EDIT
OK this is what it should have been
using (Stream stream = File.Open("main.xml", FileMode.Open))
If the file you're writing is always empty, it sounds like you aren't actually getting any of it. The first thing to check would be to see if that while (contentLength != 0) is ever entered in the first place.
Update: Also, as John pointed out, the request stream assignment is bad!
I have zip file in ftp server,this zip file contain more then one xml file in it,i want to download this zipfile and save to local disk,i have write below code,i download file but when i try to Extract this zip it throw me error that File is corrupted......
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + file);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
StreamWriter writer = new StreamWriter(destination);
writer.Write(reader.ReadToEnd());
writer.Close();
reader.Close();
response.Close();
You are channeling the result of the FTP request through a StreamReader. This has the usually beneficial effect of handling character encoding, but is not something you ever want to do when dealing with binary data (i.e. a zip file). Instead, you should read the data directly from the stream. Something like:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + file);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Copy the data from the responseStream to destination 1k at a time (feel free to increase buffer size)
byte[] buffer = new byte[1024];
for (int amountRead = responseStream.Read(buffer, 0, buffer.Length); amountRead > 0; amountRead = responseStream.Read(buffer, 0, buffer.Length))
{
destination.Write(buffer, 0, amountRead);
}
destination.Flush();
response.Close();
Here are sample code for Download file from FTP Server
Uri url = new Uri("ftp://ftp.demo.com/file1.txt");
if (url.Scheme == Uri.UriSchemeFtp)
{
FtpWebRequest objRequest = (FtpWebRequest)FtpWebRequest.Create(url);
//Set credentials if required else comment this Credential code
NetworkCredential objCredential = new NetworkCredential("FTPUserName", "FTPPassword");
objRequest.Credentials = objCredential;
objRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse objResponse = (FtpWebResponse)objRequest.GetResponse();
StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
byte[] buffer = new byte[16 * 1024];
int len = 0;
FileStream objFS = new FileStream(Server.MapPath("file1.txt"), FileMode.Create, FileAccess.Write, FileShare.Read);
while ((len = objReader.BaseStream.Read(buffer, 0, buffer.Length)) != 0)
{
objFS.Write(buffer, 0, len);
}
objFS.Close();
objResponse.Close();
}
I think that your problem is the way that you download the zip, here's an article explaining how to use it, i hope it helps:
http://www.vcskicks.com/download-file-ftp.php
Also, there is a question like this here:
how to download compressed file (.zip) through FTP using c#?
I’m using the following code to download a file from a remote ftp server:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);
request.KeepAlive = true;
request.UsePassive = true;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
using (StreamWriter destination = new StreamWriter(destinationFile))
{
destination.Write(reader.ReadToEnd());
destination.Flush();
}
The file that I’m downloading is a dll and my problem is that it is being altered by this process in some way. I know this because the file size is increasing. I have a suspicion that this section of code is at fault:
destination.Write(reader.ReadToEnd());
destination.Flush();
Can anyone offer any ideas as to what may be wrong?
StreamReader and StreamWriter work with character data, so you are decoding the stream from bytes to characters and then encoding it back to bytes again. A dll file contains binary data, so this round-trip conversion will introduce errors. You want to read bytes directly from the responseStream object and write to a FileStream that isn't wrapped in a StreamWriter.
If you are using .NET 4.0 you can use Stream.CopyTo, but otherwise you will have to copy the stream manually. This StackOverflow question has a good method for copying streams:
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read(buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write(buffer, 0, read);
}
}
So, your code will look like this:
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (FileStream destination = File.Create(destinationFile))
{
CopyStream(responseStream, destination);
}