HTTP 檔案上傳與下載

1 篇文章 / 0 new
author
HTTP 檔案上傳與下載
public partial class HttpFileForm : Form
{
    public HttpFileForm()
    {
        InitializeComponent();
    }
 
    private void btnSaveFile_Click(object sender, EventArgs e)
    {
        if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            this.tbSavePath.Text = this.saveFileDialog1.FileName;
        }
    }
 
    private void btnDownload_Click(object sender, EventArgs e)
    {
        if (this.tbSavePath.Text == "" | this.tbDownloadUrl.Text == "")
            return;
        WebClient client = new WebClient();
        string url = this.tbDownloadUrl.Text;
        string saveFile = this.tbSavePath.Text;
        try
        {
            client.DownloadFile(url,saveFile);
            MessageBox.Show("下載完成","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception err)
        {
            MessageBox.Show("下載失敗" + err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
 
    private void btnUploadFile_Click(object sender, EventArgs e)
    {
        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            this.tbUploadFile.Text = this.openFileDialog1.FileName;
        }
    }
    private void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.tbUploadFile.Text == "" | this.tbUploadUrl.Text == "")
            return;
        WebClient client = new WebClient();
        string url = this.tbUploadUrl.Text;
        string uploadFile = this.tbUploadFile.Text;
        byte[] response = null;
        try
        {
            response = client.UploadFile(url, uploadFile);
            MessageBox.Show("上傳完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception err)
        {
            MessageBox.Show("上傳失敗:"+err.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        finally
        {
            MessageBox.Show("Server回覆:\n" + System.Text.Encoding.ASCII.GetString(response));
        }
    }
}
Free Web Hosting