I am using ASP.Net MVC 4 and WebImage helpers. I need to upload my WebImage object via FTP. I just don't know how to get the WebImage parameters to send to my RequestStream.Write
Here is some code...
WebImage photo = WebImage.GetImageFromRequest();
string fileName = System.IO.Path.GetFileName(photo.FileName);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FTPaddress +"/images/" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("****", "*****");
request.UsePassive = true;
request.ContentLength = ???????;
byte[] buffer = ????????; //It's not photo.GetBytes;
Stream requestStream = request.GetRequestStream();
requestStream.Write(buffer, 0, request.ContentLength);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();
And will I be able to use the photo.Save() to the FTP path afterwards?
byte [] fileContents = photo.GetBytes();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
That's how you do it, silly me.
Related
I'm using the following code to send an XML file by FTP:
var request = (FtpWebRequest) WebRequest.Create(destination);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(UserName, Password);
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(originpath + filename))
{
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(fileContents, 0, fileContents.Length);
}
This seems to work, except the format of the file at the origin is utf-8 with signature, and it's without signature at the destination.
I've tried to preserve the utf encoding, but I seem to have lost the BOM.
Is this a problem with my code - specifically, with the way that requeststream.write works?
Two questions here :
1)difference between WebRequestMethods.Ftp.uploadfile and WebRequestMethods.Ftp.uploadfilewithuniquename?
2)When i do an upload file using the code below for an already existing file would it override the file.And is it safe to assume that it would always override?
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// what if i use request.Method = WebRequestMethods.Ftp.UploadFilewithuniquename;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
These methods refer to the FTP commands STOR and STOU.
If the logged in user has the privs, then STOR (WebRequestMethods.Ftp.UploadFile) will create a new file or overwrite an existing file.
I use following code to upload *.xls file into FTP. It works fine for uploading, but the content of *.xls file in FTP all mess up
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(#"ftp://www.xxxxxx.com" + fileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
StreamReader sourceStream = new StreamReader(filePath);
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
Any one know how to fix this problem???????
Insted of this:
StreamReader sourceStream = new StreamReader(filePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
Try to use this:
byte[] fileContents = File.ReadAllBytes(filePath);
It might be encoding\decoding problem.
I using the following code to send an excel file to FTP. File is sending, File size is also same. But file contains only spaces.
ftpAddress = "X.X.X.X";
outFilePath = "MyFolder/Sample.xls";
inFilePath = "D:/Hello.xls";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpAddress + "/" + outFilePath);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(userId, password);
//FileStream stream = File.OpenRead(inFilePath);
byte[] fileContents = File.ReadAllBytes(inFilePath);
//byte[] buffer = new byte[stream.Length];
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
//Shows confirm message
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine(response);
response.Close();
Please help. Thanks in advance.
Source: http://msdn.microsoft.com/en-us/library/ms229715(v=vs.110).aspx
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe#contoso.com");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("testfile.txt");
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
}
}
My Class FTP work fine wtih my FTP server, but not with my client FTP server.
public class UploadToFTP
{
public void UploadFTP(string LeSource, string Desti, string CodeClient)
{
Pers_Conf oConf = LeConf.Get_Config(CodeClient);
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + oConf.FtpServer + oConf.FtpChemin +"/" + Desti);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(oConf.FtpLogin, oConf.FtpPwd);
request.UseBinary = true;
request.UsePassive = true;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(LeSource);
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();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
}
this line is not work : Stream requestStream = request.GetRequestStream();
Anybody have solution ?
i have change the right into 777.
Thanks you in advance,
Stev
The Solutions
/ work only several ftp servers, but // it works perfectly everywhere