Wednesday, July 28, 2010

System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission,

Upload files to the FTP server via C#.NET code is a simple task when you use System.Net and System.IO namespaces. But when I was trying to do that I get an opportunity to know few more things that I didn’t know before.
NOTE: if you are going to use this FTP upload in commercial hosting server like GoDaddy then please make sure weather you can have the access to do so.
In my experience I could not run my web application in GoDaddy Server. That gave me exception “ System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=……
Even it is working FTP upload in your local machine, when you upload it to the GoDaddy that will not work. It is occurring because of the permission gave by GoDadday to your web application. We talk to the Godaddy and they inform us, they can not allow us to do our operation, because we are using shared hosting server.
When I search an answer for that most of the blogs and forums said they used some other Hosting plan to overcome that.
If you are interesting to know the FTP upload via C# .NET
File Upload in to FTP Server via C#.NET Code
File Download From FTP Server via C#.NET Code






C#.NET FTP File Upload

C#.NET gives the namespace to upload Files to FTP System.NET following custom method can be use to upload a file in to a FTP Server
public static void UploadToFTP(string FTPAddress, string FileName, string FullPath, string UserName, string Password)
{
       /*
        * FTPAddress - URL or IP address of the FTP server
        * FileName - Name of the File which is going to upload
        * FullPath - You have to give the Absolute path to the File you are going to upload
        * UserName - User name for the FTP account
        * Password - FTP Password
        */
System.Net.FtpWebRequest objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPAddress, FileName));

if (objWebRequest != null)
{
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = false;

byte[] FileBuffer = null;
//Load The File To Mememory

try
{
   //open and Read the File in To the Memory
   using (System.IO.FileStream objFStream = System.IO.File.OpenRead(FullPath))
  {
     FileBuffer = new byte[objFStream.Length];

    objFStream.Read(FileBuffer, 0, FileBuffer.Length);
    objFStream.Close();
  }

    //Upload File To the FTP
    using (System.IO.Stream objStream = objWebRequest.GetRequestStream())
   {
       if (FileBuffer != null)
       {
           objStream.Write(FileBuffer, 0, FileBuffer.Length);
           objStream.Close();
       }
    }
}
 catch (Exception Ex)
 {
    throw Ex;
 }
}


}

Did you get this Error Exception? “System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, …..Then your have to read this Post too

You will be able to download a good windows application to the this purpose from http://www.vcskicks.com/download-file-ftp.php that was really help full to me thanks should goes to the people who ever wrote that article






Tuesday, July 27, 2010

FTP File Download C#.NET

You can download files from FTP using following custom Method
public static string FTPDownload(string FTPURL, string FileName, string SavingPathonServer, string UserName, string Password)
{
/*
* FTPAddress - URL or IP address of the FTP server
* FileName - Name of the File which is going to upload
* SavingPathonServer - This method will save Specifide file in to the WebServer you have to give the path to save on web server
* UserName - User name for the FTP account
* Password - FTP Password
*/

byte[] bteDownloadedData = new byte[0];
string strServerPathToFile = string.Empty;
try
{

System.Net.FtpWebRequest objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPURL, FileName));
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.GetFileSize;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = true;

int intDataLength = (int)objWebRequest.GetResponse().ContentLength;


objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPURL, FileName));
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = false;

System.Net.FtpWebResponse objFTPWebResponse =(System.Net.FtpWebResponse)objWebRequest.GetResponse();
System.IO.Stream objStreamReader = objFTPWebResponse.GetResponseStream();

System.IO.MemoryStream objMemStream = new System.IO.MemoryStream();
byte[] memBuffer = new byte[1024];

int intBytesRead = 0;

while (true)
{
intBytesRead = objStreamReader.Read(memBuffer, 0, memBuffer.Length);
if (intBytesRead != 0)
{
objMemStream.Write(memBuffer, 0, intBytesRead);
}
else
{
break;
}

}

bteDownloadedData = objMemStream.ToArray();
objStreamReader.Close();
objMemStream.Close();
objFTPWebResponse.Close();

if (bteDownloadedData != null && bteDownloadedData.Length != 0)
{
System.IO.FileStream objFileStream = new System.IO.FileStream(SavingPathonServer, System.IO.FileMode.Create);
objFileStream.Write(bteDownloadedData, 0, bteDownloadedData.Length);
objFileStream.Close();
}
return SavingPathonServer;

}
catch (Exception Ex)
{
throw Ex;
}

}


}
}

Did you get this Error Exception? “System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, …..” Then your have to read this Post too

Your will be able to download a good windows application to the this purpose from http://www.vcskicks.com/csharp_ftp_upload.php that was really help full to me thanks should goes to the people who ever wrote that article