修改FileBrowser/fileworkerbase.cs的FileUpload方法:
protected void FileUpload( string resourceType, string currentFolder, bool isQuickUpload )
{
HttpPostedFile File = Request.Files[ "NewFile" ];
System.Drawing.Image img = System.Drawing.Image.FromStream(oFile.InputStream);
string sFileName = "";
if ( File == null )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}
// Map the virtual path to the local server path.
string sServerDir = this.ServerMapFolder( resourceType, currentFolder, isQuickUpload );
// Get the uploaded file name.
sFileName = System.IO.Path.GetFileName( oFile.FileName );
sFileName = this.SanitizeFileName( sFileName );
string sExtension = System.IO.Path.GetExtension( oFile.FileName );
sExtension = sExtension.TrimStart( '.' );
if ( !this.Config.TypeConfig[ resourceType ].CheckIsAllowedExtension( sExtension ) )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}
if ( this.Config.CheckIsNonHtmlExtension( sExtension ) && !this.CheckNonHtmlFile( oFile ) )
{
this.SendFileUploadResponse( 202, isQuickUpload );
return;
}
int iErrorNumber = 0;
int iCounter = 0;
string date = DateTime.Now.ToString("yyyyMMddhhmmss");
string sFileNameAll = System.IO.Path.GetFileName(oFile.FileName);
string imageFullName = date + "_" + sFileNameAll;
string rginalNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(imageFullName);
while ( true )
{
string sFilePath = System.IO.Path.Combine(sServerDir, imageFullName);
if ( System.IO.File.Exists( sFilePath ) )
{
iCounter++;
imageFullName =
orginalNameWithoutExt +
"(" + iCounter + ")." +
sExtension;
iErrorNumber = 201;
}
else
{
bool con = ftpuploadAll(img, image_xq + imageFullName);//上传到ftp
if (con == false)
{
iErrorNumber = 400;
}
else
{
oFile.SaveAs(sFilePath);//上传到本地
iErrorNumber = 0;
}
break;
}
}
TypeConfig typeConfig = this.Config.TypeConfig[resourceType] ;
string sFileUrl = isQuickUpload ? typeConfig.GetQuickUploadPath() : typeConfig.GetFilesPath() ;
sFileUrl += imageFullName;
this.SendFileUploadResponse(iErrorNumber, isQuickUpload, sFileUrl, imageFullName);
}
FTP方法:
在webconfig配置ftp的信息,在这里调用
public static readonly string ftpuser = ConfigurationManager.AppSettings["ftp_user"].ToString();//ftp user
public static readonly string ftppassword = ConfigurationManager.AppSettings["ftp_password"].ToString();//ftp password
public static readonly string image_xq = ConfigurationManager.AppSettings["image_xq"].ToString();//ftp address
public bool ftpuploadAll(System.Drawing.Image image, string uri)
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpuser, ftppassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
//实例化流
MemoryStream imageStream = new MemoryStream();
//将图片的实例保存到流中
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
//保存流的二进制数组
byte[] imageContent = new Byte[imageStream.Length];
imageStream.Position = 0;
//将流泻如数组中
imageStream.Read(imageContent, 0, (int)imageStream.Length);
byte[] buff = imageStream.ToArray();
try
{
Stream strm = reqFTP.GetRequestStream();
strm.Write(buff, 0, buff.Length);
strm.Close();
}
catch (Exception ex)
{
return false;
}
return true;
}