MS Knowledge Base: Bagaimana cara untuk meng-upload and download files dari remote server di ASP.NET? (Part 2)

Diskusi Umum MS Knowledge Base: Bagaimana cara untuk meng-upload and download files dari remote server di ASP.NET? (Part 2)

  • 31 Mei 2011 3:47
    Moderator
     
      Memiliki Kode

    RemoteDownload class

     
    Kelas RemoteDownload juga memiliki dua kelas anak. Kelas-kelas ini
    adalah HttpRemoteDownload dan FtpRemoteDownload. Kelas RemoteDownload membutuhkan sumber daya URI dan direktori setempat. Kelas RemoteDownload melakukan pengecekan untuk memastikan bahwa sumber daya URI tersedia  sebelum download dimulai. Kelas mengambil stream yang berisi data respon dari server, dan kemudian menulis array byte ini ke FILESTREAM. Kelas FtpRemoteDownload menggunakan FtpWebRequest langsung (daripada menggunakan kelas WebClient tingkat yang lebih tinggi) untuk menangani kebutuhan spesifik dari protokol FTP.

    Lihat definisi
    kelas berikut untuk informasi lebih lanjut tentang kelas RemoteDownload: 

     

    public class HttpRemoteDownload : RemoteDownload
        {
            public HttpRemoteDownload(string urlString, string descFilePath)
                : base(urlString, descFilePath)
            {
     
            }
     
            public override bool DownloadFile()
            {
                string fileName = System.IO.Path.GetFileName(this.UrlString);
                string descFilePathAndName =
                    System.IO.Path.Combine(this.DescFilePath, fileName);
                try
                {
                    WebRequest myre = WebRequest.Create(this.UrlString);
                }
                catch
                {
                    return false;
                }
                try
                {
                    byte[] fileData;
                    using (WebClient client = new WebClient())
                    {
                        fileData = client.DownloadData(this.UrlString);
                    }
                    using (FileStream fs =
                          new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                    {
                        fs.Write(fileData, 0, fileData.Length);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    throw new Exception("download field", ex.InnerException);
                }
            }
        }
     
    public class FtpRemoteDownload : RemoteDownload
        {
            public FtpRemoteDownload(string urlString, string descFilePath)
                : base(urlString, descFilePath)
            {
     
            }
     
            public override bool DownloadFile()
            {
                FtpWebRequest reqFTP;
     
                string fileName = System.IO.Path.GetFileName(this.UrlString);
                string descFilePathAndName =
                    System.IO.Path.Combine(this.DescFilePath, fileName);
     
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    
                    using (FileStream outputStream = new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                    using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse())
                    using (Stream ftpStream = response.GetResponseStream())
                    {
                        int bufferSize = 2048;
                        int readCount;
                        byte[] buffer = new byte[bufferSize];
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                        while (readCount > 0)
                        {
                            outputStream.Write(buffer, 0, readCount);
                            readCount = ftpStream.Read(buffer, 0, bufferSize);
                        }
                    }
                    return true;
                }
     
                catch (Exception ex)
                {
                    throw new Exception("upload failed", ex.InnerException);
                }
            }
        }

    Catatan Untuk informasi lebih lanjut tentang cara membuat sampel aplikasi dan bagaimana cara menggunakan sampel aplikasi, lihat file readme.txt yang disertakan dalam paket download.

     

    Bahasa Pemograman

     

    Contoh kode ini memuat bahasa pemograman dibawah ini:Collapse this tableExpand this tableCollapse this tableExpand this tableCollapse this tableExpand this tableCollapse this tableExpand this table

    Language

    Project Name

    Visual C#

    CSRemoteUploadAndDownload

    Visual Basic.NET

    VRemoteUploadAndDownload

    Kategori Teknologi

    ·         ASP.NET 2.0

    ·         ASP.NET 3.5

    ·         ASP.NET 4.0

    Link

    For more information about the WebClient class, visit the following Microsoft Developer (MSDN) website:

    General information about the WebClient class (http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx)

    For more information about the UploadData method, visit the following MSDN website:

    General information about the UploadData method (http://msdn.microsoft.com/en-us/library/system.net.webclient.uploaddata.aspx)

    For more information about the DonwloadData method, visit the following MSDN website:

    General information about the DonwloadData method (http://msdn.microsoft.com/en-us/library/system.net.webclient.downloaddata.aspx)

    For more information about the FtpWebRequest method, visit the following MSDN website:

    General information about the FtpWebRequest method (http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx)

    For more information about how to upload Files with FTP, visit the following MSDN website:

    How to upload Files with FTP (http://msdn.microsoft.com/en-us/library/ms229715.aspx)

    Referensi

    http://support.microsoft.com/kb/2512241/en-us


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.