if (fileupload.HasFile) { //create the path to save the file to string fileName = "/" + fileupload.FileName; //Is the file too big to upload? int fileSize = fileupload.PostedFile.ContentLength; string maxsize = ConfigurationManager.AppSettings["MaxFileSize"]; bool sflag = Convert.ToBoolean(ConfigurationManager.AppSettings["RestrictFileSize"]); if (sflag) { if (fileSize > (Convert.ToInt32(maxsize) * 1048576)) { UploadStatusLabel.Text = "Filesize is too large. Maximum file size permitted is " + maxsize + "MB"; } else { ////save the file to our local path string foldername = string.Empty; string ftpUser = string.Empty; string ftpPassword = string.Empty; foldername = ConfigurationManager.AppSettings["FilePath"]; ftpUser = ConfigurationManager.AppSettings["FTPUser"]; ftpPassword = ConfigurationManager.AppSettings["FTPPassword"]; string userdomain = ConfigurationManager.AppSettings["FTPDomain"]; string filename = fileupload.PostedFile.FileName; FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(foldername)+ fileName); request.Method = WebRequestMethods.Ftp.UploadFile; // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential(ftpUser.Normalize(), ftpPassword.Normalize(), userdomain.Normalize()); // Copy the contents of the file to the request stream. StreamReader sourceStream = new StreamReader(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(); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); response.Close(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)