Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace UploadFileOnServer
- {
- public class UploadFileClass
- {
- private static string GetValidFileName(string fileName)
- {
- // remove any invalid character from the filename.
- return Regex.Replace(fileName.Trim(), "[^A-Za-z0-9_ ]+", "");
- }
- public string UploadFile(FileUpload FU, string FolderID)
- {
- string[] validFileTypes = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "docx", "xls", "xlsx", "pdf", "rar", "zip", "ppt" };
- string ext = System.IO.Path.GetExtension(FU.PostedFile.FileName).ToLower();
- bool isValidFile = false;
- for (int i = 0; i < validFileTypes.Length; i++)
- {
- if (ext == "." + validFileTypes[i])
- {
- isValidFile = true;
- break;
- }
- }
- if (!isValidFile)
- {
- return "0";
- }
- else
- {
- string namefile = System.IO.Path.GetFileNameWithoutExtension(FU.FileName);
- string file_name = GetValidFileName(namefile);
- string remoteFilename = file_name + ext;
- string ftpUserName = System.Configuration.ConfigurationManager.AppSettings["FTP_UserName"].ToString();
- string ftpPassword = System.Configuration.ConfigurationManager.AppSettings["FTP_Password"].ToString();
- string FTP_Path = System.Configuration.ConfigurationManager.AppSettings["FTP_Path"].ToString();
- string HTTP_Path = System.Configuration.ConfigurationManager.AppSettings["HTTP_Path"].ToString();
- FtpWebResponse CreateFolderResponse;
- try
- {
- //fname = ContentUpload.FileName;
- string ftpfullpath = FTP_Path + FolderID + "/";
- //Create the folder, Please notice if the WPF folder already exist, it will result 550 error.
- FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
- ftp.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
- ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
- CreateFolderResponse = (FtpWebResponse)ftp.GetResponse();
- if (CreateFolderResponse.StatusCode == FtpStatusCode.PathnameCreated)
- {
- string uri = FTP_Path + FolderID + "/" + remoteFilename;
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
- reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
- reqFTP.KeepAlive = false;
- reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
- reqFTP.UseBinary = true;
- reqFTP.ContentLength = FU.PostedFile.ContentLength;
- Stream OutputStream = reqFTP.GetRequestStream();
- int buffLength = 2048000;
- byte[] buff = new byte[buffLength];
- int contentLen = (int)FU.PostedFile.InputStream.Length;
- Stream InputStream = FU.PostedFile.InputStream;
- contentLen = InputStream.Read(buff, 0, buffLength);
- while (contentLen != 0)
- {
- OutputStream.Write(buff, 0, contentLen);
- contentLen = InputStream.Read(buff, 0, buffLength);
- }
- InputStream.Close();
- OutputStream.Close();
- }
- }
- catch
- {
- try
- {
- string uri = FTP_Path + FolderID + "/" + remoteFilename;
- FtpWebRequest reqFTP;
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
- reqFTP.KeepAlive = false;
- reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
- reqFTP.UseBinary = true;
- reqFTP.ContentLength = FU.PostedFile.ContentLength;
- Stream OutputStream = reqFTP.GetRequestStream();
- int buffLength = 2048000;
- byte[] buff = new byte[buffLength];
- int contentLen = (int)FU.PostedFile.InputStream.Length;
- Stream InputStream = FU.PostedFile.InputStream;
- contentLen = InputStream.Read(buff, 0, buffLength);
- while (contentLen != 0)
- {
- OutputStream.Write(buff, 0, contentLen);
- contentLen = InputStream.Read(buff, 0, buffLength);
- }
- InputStream.Close();
- OutputStream.Close();
- }
- catch
- {
- }
- }
- return remoteFilename;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment