I did the same with my code and it works fine. Maybe its helpful for you:
public static bool UploadFileUsingFtp(string fileName, string completeLocalPath)
{
string ftpServer = GetConfig("FTPServer");
string userName = GetConfig("FTPUserName");
string password = GetConfig("FTPPassword");
var reqObj = WebRequest.Create(Path.Combine(ftpServer, fileName)) as FtpWebRequest;
try
{
//Call A FileUpload Method of FTP Request Object
if (reqObj != null)
{
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
reqObj.EnableSsl = bool.Parse(GetConfig("EnableSsl"));
//If you want to access Resourse Protected You need to give User Name and PWD
reqObj.Credentials = new NetworkCredential(userName, password);
}
//FileStream object read file from Local Drive
FileStream streamObj = File.OpenRead(completeLocalPath);
//Store File in Buffer
byte[] buffer = new byte[streamObj.Length + 1];
//Read File from Buffer
streamObj.Read(buffer, 0, buffer.Length);
//Close FileStream Object Set its Value to nothing
streamObj.Close();
streamObj = null;
//Upload File to ftp://localHost/ set its object to nothing
if (reqObj != null) reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);
reqObj = null;
return true;
}
catch (Exception ex)
{
// return false;
throw new Exception(ex.Message);
}
finally
{
reqObj = null;
}
}
sunilexplore...
0 Points
2 Posts
Upload a file on HTTPS server
Mar 30, 2012 09:28 AM|LINK
Hi All,
I am trying to upload a file on HTTPS server.
File location : C:\data.text
Url: https://abc.com/Application/UploadLocation/
User ID: XXX
Password: XXX
I am using the below code:
public void UploadY() { StreamReader srDel = new StreamReader("C:\\data.txt"); string sTestDelete = srDel.ReadToEnd(); srDel.Close(); string sDeleteURL = "https://abc.com/Application/UploadLocation/"; byte[] AuthBytes = Encoding.ASCII.GetBytes("uname:password"); string sAuth = Convert.ToBase64String(AuthBytes); HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(sDeleteURL); HttpWebResponse oResponse = null; Request.Method = "POST"; // Request.Headers.Add("ContentType", "application/xml"); Request.Headers.Add("ContentType", "text/xml"); Request.Headers.Add("Authorization", "Basic " + sAuth); Request.Headers.Add("dev-t", " OurDevToken "); Request.Headers.Add("UploadFor", "Marketplace"); string cerPath = @"C:\TestCert\cerNew.cer"; System.Security.Cryptography.X509Certificates.X509Certificate ClientCertificate = new System.Security.Cryptography.X509Certificates.X509Certificate(cerPath); Request.ClientCertificates.Add(ClientCertificate); byte[] SendByteArray = Encoding.GetEncoding(1252).GetBytes(sTestDelete); Request.ContentLength = SendByteArray.Length; Stream streamPostData = Request.GetRequestStream(); streamPostData.Write(SendByteArray, 0, SendByteArray.Length); streamPostData.Close(); oResponse = (HttpWebResponse)Request.GetResponse(); StreamReader responseStream = new StreamReader(oResponse.GetResponseStream(), Encoding.ASCII); string sResponse = responseStream.ReadToEnd(); System.Web.HttpContext.Current.Response.Write(sResponse); oResponse.Close(); responseStream.Close(); }Bui I am getting the below error:
{"The remote server returned an error: (405) Method Not Allowed."}
I tried to google it but could’nt find the solution.
If anyone could put some light on this problem.
d4daud
Contributor
2293 Points
468 Posts
Re: Upload a file on HTTPS server
Mar 30, 2012 09:35 AM|LINK
I did the same with my code and it works fine. Maybe its helpful for you:
public static bool UploadFileUsingFtp(string fileName, string completeLocalPath) { string ftpServer = GetConfig("FTPServer"); string userName = GetConfig("FTPUserName"); string password = GetConfig("FTPPassword"); var reqObj = WebRequest.Create(Path.Combine(ftpServer, fileName)) as FtpWebRequest; try { //Call A FileUpload Method of FTP Request Object if (reqObj != null) { reqObj.Method = WebRequestMethods.Ftp.UploadFile; reqObj.EnableSsl = bool.Parse(GetConfig("EnableSsl")); //If you want to access Resourse Protected You need to give User Name and PWD reqObj.Credentials = new NetworkCredential(userName, password); } //FileStream object read file from Local Drive FileStream streamObj = File.OpenRead(completeLocalPath); //Store File in Buffer byte[] buffer = new byte[streamObj.Length + 1]; //Read File from Buffer streamObj.Read(buffer, 0, buffer.Length); //Close FileStream Object Set its Value to nothing streamObj.Close(); streamObj = null; //Upload File to ftp://localHost/ set its object to nothing if (reqObj != null) reqObj.GetRequestStream().Write(buffer, 0, buffer.Length); reqObj = null; return true; } catch (Exception ex) { // return false; throw new Exception(ex.Message); } finally { reqObj = null; } }sunilexplore...
0 Points
2 Posts
Re: Upload a file on HTTPS server
Apr 02, 2012 06:39 AM|LINK
Hi
Thanks for your response.
But I would like to use Just HTTPS Url path and not FTP protocol.
say file on local: c:\apple.txt
Upload location on https server: https://abc.com/upload/