Upload xml file on ftp binary - c#

i have code, where i send xml file to ftp server, but size of file on ftp server is smaller than original file. I'm trying to enable binary transmission, but result is still the same.
FileInfo f = new FileInfo("C:\\Users\\L\\Desktop\\data.xml");
long original_vel = f.Length;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://***");
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("*****", "*****");
StreamReader sourceStream = new StreamReader(#"C:\\Users\\L\\Desktop\\data.xml");
byte[] fileContents = Encoding.Unicode.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
long ftp_vel = request.ContentLength;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (original_vel == ftp_vel)
{
response.Close();
}
else
{
Odesilani();
}
The size of original file is 294 672, but the file on ftp have 294 670.
The xml file on ftp is valid....But when i compare files in total comander, the original file have: FF FE 3C 00 3F 00.....and the file on ftp have 3C 00 3F 00...But the content of file is ok...:/
Have you any idea?

Is the XML file at the server valid? From your code, You are reading the file using Unicode. Files that are encoded using unicode usually have a character that is placed at the beginning of the file called the Byte Order Mark . That may be the reason you have a 2-Byte difference as it was lost during conversion.
UPDATE The Proper Byte Order Mark for any encoding is given by Encoding.GetPreamble()
The fix to the code above would be..
StreamReader sourceStream = new StreamReader(#"C:\\Users\\L\\Desktop\\data.xml");
//Get Preamble and File Contents
byte[] bom = Encoding.Unicode.GetPreamble();
byte[] content = Encoding.Unicode.GetBytes(sourceStream.ReadToEnd());
//Create Destination array
byte[] fileContents = new Byte[bom.Length + content.Length];
//Copy arrays into destination appending bom if available
Array.Copy(bom, 0, fileContents, 0, bom.Length);
Array.Copy(content, 0, fileContents, bom.Length, content.Length);
request.ContentLength = fileContents.Length;
long ftp_vel = request.ContentLength;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
if (original_vel == ftp_vel)
{
response.Close();
}
else
{
Odesilani();
}

Related

How to upload large files to FTP server in ASP MVC

I am developing an ASP MVC website.
Now i need to upload files(.zip files) to my FTP server. For uploading i use this code.
This code uploads only those files which has size < 10 Mb.
for Example: when i upload a file with 150 MB size with this code it get damaged and the file size changed to 300 MB on my Ftp Server.
So can any one help me..
byte[] fileBytes = null;
//Read the FileName and convert it to Byte array.
string filename = Path.GetFileName(FileUpload1.FileName);
using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
{
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
fileStream.Close();
}
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + "/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential(ftpUName, ftpPWord);
request.ContentLength = fileBytes.Length;
request.UsePassive = true;
request.UseBinary = true;
request.ServicePoint.ConnectionLimit = fileBytes.Length;
request.EnableSsl = false;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileBytes, 0, fileBytes.Length);
requestStream.Close();
}
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
Add this to your web.config
<httpRuntime maxRequestLength="whatever value you need in kb max value is 2,147,483,647 kb" relaxedUrlToFileSystemMapping="true" />
under system.web section
the default is 4 mb size limit
Details here
It probably gets corrupt, because you are reading the data with utf8 encoded. You should read it in binary format.
Don't use:
using (StreamReader fileStream = new StreamReader(FileUpload1.InputStream))
{
fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
fileStream.Close();
}
You have to use File.ReadAllBytes or a BinaryReader(Stream)
https://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx
https://msdn.microsoft.com/de-de/library/system.io.binaryreader(v=vs.110).aspx
for your example:
byte[] fileBytes = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)...

Exporting byte[] results to corrupted .zip

I am trying to make a CLR with .NET 2.0 integrated into MS SQL Server 2008. I call an API with and I should receive a .zip as response. I store the response into a Stream and I want to export this file to a physical .zip file.
I tried exporting the file with C# and SQL (BCB OR OLE) and all resulted into a corrupted file. So, I believe I am doing something wrong with in the making of the stream.
The C# code is the following:
private static byte[] GetStreamFileResult(Cookie loginCookie, Guid fileGuid, String baseUri)
{
byte[] output = null;
//String result = null;
string url = "some url"
CookieContainer cookies = new CookieContainer();
cookies.Add(new Uri(url), loginCookie);
WebRequest request = WebRequest.Create(url);
(request as HttpWebRequest).CookieContainer = cookies;
WebResponse response = request.GetResponse();
HttpWebResponse resp = response as HttpWebResponse;
Stream dataStream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
CopyStream(dataStream, ms);
output = ms.ToArray();
}
dataStream.Close();
response.Close();
return output;
}
The C# code to export the zip is the following:
File.WriteAllBytes("C:\\folder\\t.zip", stream); // Requires System.IO
The copy from stream to stream:
public static void CopyStream(Stream input, Stream output)
{
if (input != null)
{
using (StreamReader reader = new StreamReader(input))
using (StreamWriter writer = new StreamWriter(output))
{
writer.Write(reader.ReadToEnd());
}
}
}
Your CopyStream is broken. You need to talk binary. You're currently treating binary zip data as though it were text:
byte[] buffer = new byte[2048];
int bytesRead;
while((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, bytesRead);
}

How to save csv to memory and then ftp?

I am currently creating a CSV file and then I ftp that file.
This is working fine. However, I dont want to save the csv file, i want to create it to memory and then ftp it.
This is my current code:
private void Csv()
{
CsvExport eftExport = new CsvExport();
eftExport.AddRow();
eftExport["customer_reference"] = "Ref";
eftExport["landline"] = "01234567890";
string url = "C:/Content/Cms/DD/";
string fileName = "file.csv";
eftExport.ExportToFile(url + fileName);
this.FtpFile(url, fileName);
}
private void FtpFile(string url, string fileName)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://url.co.uk/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential ("Administrator", "pass");
StreamReader sourceStream = new StreamReader(url + fileName);
byte[] fileContents = Encoding.UTF8.GetBytes (sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
}
but in stead of doing eftExport.ExportToFile(url + fileName); i dont want it to save to machine??
Use this to but it into a byte array:
byte[] buffer = eftExport.ExportToBytes();
Now:
requestStream.Write(buffer, 0, buffer.Length);
Use the ExportToBytes() function of your CsvExport class.
Then change your FtpFile() to accept a byte array and remove the stream reader
you should end up with quite a bit less code :)
If your CsvExport type has an ExportToStream or similar simply use that create the stream that you subsequently write to the requestStream.

Encrypt File As Posted to FTP Site

I'm using VS 2010 (C#). I'm trying to encrypt (decrypt) a file as it is being uploaded (downloaded) from an FTP site. I thought this would be quicker than using a local temp file to encrypt before upload and decrypt after download. I get an error on the code below. I just can't seem to get the various stream types in alignment (i.e. FileStream, CryptoStream, and Stream). Any help is much appreciated.
public void Upload(byte[] desKey, byte[] desIV)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Destination);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(UserName, Password);
FileStream fStream = File.Open(SourceFile, FileMode.OpenOrCreate);
CryptoStream responseStream = new CryptoStream(fStream, new DESCryptoServiceProvider().CreateDecryptor(desKey, desIV), CryptoStreamMode.Read);
byte[] fileContents = Encoding.UTF8.GetBytes(responseStream.ToString());
responseStream.Close(); ///ERROR here
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}
Unit Test:
public void CleanEncryptUploadTest()
{
FT.ftp uploadTest = new FT.ftp();
uploadTest.UserName = "ausername";
uploadTest.Password = "apassword";
uploadTest.SourceFile = "D:\\Temp\\Test\\file.txt";
uploadTest.Destination = "ftp://ftp.mysite.com/test2.txt";
byte[] key = ASCIIEncoding.ASCII.GetBytes("TestZone");
byte[] initVector = ASCIIEncoding.ASCII.GetBytes("TestZone");
uploadTest.Upload(key, initVector);
}
This worked for me, I used a memory stream and wrote the encrypted bytes to it instead. Also changed the cryptostream mode to write.
public void Upload(byte[] key, byte[] iv)
{
byte[] fileContents;
using (FileStream inputeFile = new FileStream(this.SourceFile, FileMode.Open, FileAccess.Read))
{
using (MemoryStream encryptedStream = new MemoryStream())
{
using (CryptoStream cryptostream = new CryptoStream(encryptedStream, new DESCryptoServiceProvider().CreateEncryptor(key, iv), CryptoStreamMode.Write))
{
byte[] bytearrayinput = new byte[inputeFile.Length];
inputeFile.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
fileContents = encryptedStream.ToArray();
}
}
}
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(this.Destination);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(this.UserName, this.Password);
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
}

FTP Changing PGP File During Transfer in C#

I have PGP files that I've verified as being valid, but at some point during the FTP upload, they become corrupt. When retrieved, I get an error message stating "Found no PGP information in these file(s)."
For what it's worth, the PGP is version 6.5.8, but I think that this is unimportant, as the files seem alright before they're uploaded.
My code is as follows for the file transfer, is there a setting or field that I've missed?
static void FTPUpload(string file)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.itginc.com" + "/" + Path.GetFileName(file));
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ApplicationSettings["Username"], ApplicationSettings["Password"]);
StreamReader sr = new StreamReader(file);
byte[] fileContents = Encoding.UTF8.GetBytes(sr.ReadToEnd());
sr.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload file complete, status {0}", resp.StatusDescription);
resp.Close();
string[] filePaths= Directory.GetFiles(tempPath);
foreach (string filePath in filePaths)
File.Delete(filePath);
}
Any help is appreciated
Hmmmm try not reading it into a byte array and instead doing something like this
using (var reader = File.Open(source, FileMode.Open))
{
var ftpStream = request.GetRequestStream();
reader.CopyTo(ftpStream);
ftpStream.Close();
}
PGP encodes data to binary stream, so your reading it via StreamReader and UTF8 probably breaks the data. FTP is unlikely to alter the data as you explicitly binary mode (though UseBinary is true by default so your setting should not do anything at all).

Categories

Resources