Murat Tüfekçi

Asp.Net FTP Upload (C#)Subscribe Subscribe

  • Murat Tüfekçi
  • April 8th, 2007

Bu yazımızda C# ile dosya upload ve ASP.NET FileUpload kontrolünden direk FTP sunucusuna dosya yüklemeyi anlatacağım.

İki senaryomuz mevcut. 1.si direk hdd den bir ftp sunucusuna dosya yüklemek, 2.si Asp.Net FileUpload kontrolu ile sunucu diskine kaydetmeden ftp ye upload etmek.

// FTPUpload Sinifi
using System;
using System.Net;
using System.IO;
using System.Web;

/// |summary>
/// FTPUpload Class
/// |/summary>
public class FTPUpload
{
private string _LocalPath;
/// |summary>
/// Disk üzerindeki Yolu Orn: C:\abc.jpg
/// |/summary>
public string LocalPath
{
get { return _LocalPath; }
set { _LocalPath = value; }
}

private string _RemoteUrl;
/// |summary>
/// Upload için URL. Orn ftp://ftp.mt.gen.tr/wwwroot
/// |/summary>
public string RemoteUrl
{
get { return _RemoteUrl; }
set { _RemoteUrl = value; }
}

private string _UserName;
/// |summary>
/// Ftp Kullanici adi
/// |/summary>
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}

private string _Password;
/// |summary>
/// Ftp Kullanici Sifresi
/// |/summary>
public string Password
{
get { return _Password; }
set { _Password = value; }
}
private string _remoteFileName;
/// |summary>
/// Upload edilen dosyanin ftp server uzerindeki resmi
/// |/summary>
public string RemoteFileName
{
get { return _remoteFileName; }
set { _remoteFileName = value; }
}

/// |summary>
/// Default Constructor
/// |/summary>
public FTPUpload()
{
this._LocalPath = string.Empty;
this._RemoteUrl = string.Empty;
this._Password = string.Empty;
this._UserName = string.Empty;
this._remoteFileName = string.Empty;
}
/// |summary>
/// Istege bagli olarak gerekli degerler constructor araciligi ilede atanabilir.
/// |/summary>
/// |param name="LocalPath">|/param>
/// |param name="RemoteURL">|/param>
/// |param name="RemoteFileName">|/param>
/// |param name="UserName">|/param>
/// |param name="Password">|/param>
public FTPUpload(string LocalPath, string RemoteURL,string RemoteFileName, string UserName, string Password)
{
this._LocalPath = LocalPath;
this._RemoteUrl = RemoteURL;
this._Password = Password;
this._UserName = UserName;
this._remoteFileName = RemoteFileName;
}

/// |summary>
/// Server da yada local diskte bulunan dosyayı ftp sunucuyu upload etmek icin
/// |/summary>
/// |returns>islem basarili sekilde tamamlandimi?|/returns>
public bool UploadFile()
{
try
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(this._RemoteUrl + @"/" + this._remoteFileName);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(this._UserName, this._Password);
FileStream fs = new FileStream(this._LocalPath, FileMode.Open);
byte[] bS = new byte[fs.Length];
fs.Read(bS, 0, bS.Length);
fs.Close();
fs.Dispose();
req.GetRequestStream().Write(bS, 0, bS.Length);
}
catch (Exception ex)
{
return false;
}
return true;
}

///
/// FileUpload kontrolunden post edilen dosyayi direk ftp ye upload etmek
/// |param name="postedFile" |FileUpload kontrolunun postedfile ozelligi |/param|
/// |/summary|
/// returns|islem basarili sekilde tamamlandimi?|/returns|
public bool UploadStreamToStream(HttpPostedFile postedFile)
{
try
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(this._RemoteUrl + @"/" + this._remoteFileName);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.Credentials = new NetworkCredential(this._UserName, this._Password);
Stream fs = postedFile.InputStream;
byte[] bS = new byte[fs.Length];
fs.Read(bS, 0, bS.Length);
fs.Close();
fs.Dispose();
req.GetRequestStream().Write(bS, 0, bS.Length);
}
catch (Exception ex)
{
return false;
}
return true;
}
}

Sinif tanimini tamamladiktan sonra .aspx.cs de herhangi bir event icerinde
//Asp.Net

protected void Button1_Click(object sender, EventArgs e)
{
FTPUpload up = new FTPUpload();
up.LocalPath = Server.MapPath("./a.gif");
up.RemoteUrl = "ftp://ftp.mt.gen.tr";
up.RemoteFileName = "abcx.gif";
up.UserName = "user";
up.Password = "pwd";
if (up.UploadStreamToStream(FileUpload1.PostedFile))
Response.Write("OK");
else
Response.Write("YOK");
}

şeklinde kullanabilirsiniz. Eğer sorunuz varsa yorum olarak yollarsanız yardım etmekten mutluluk duyarım.





Article Comments1 Comment

  • gboff

    Posted on September 5th, 2008 at 3:20 pm

    if (up.UploadStreamToStream(FileUpload1.PostedFile)) burdaki
    FileUpload1 nedir

Leave a Comment

Required
Required (Won't be published)
Optional
Avatar
Add your picture!
Join Gravatar and upload your avatar. It's free!