2009年2月10日

Silverlight File Upload


如果要利用 Silverlight 設計一個相簿的編輯工具,
你可能需要將使用者的照片上傳到 server 上。
要在 client 端選擇上傳的 file,可以使用 OpenFileDialog
OpenFileDialog dlg = new OpenFileDialog();
dlg.Multiselect = false;
dlg.Filter = "All files (*.*)*.*JPG Images (*.jpg)*.jpg";
bool? retval = dlg.ShowDialog();

if (retval != null && retval == true) 
{
    string fileName = dlg.File.Name;
    UploadFile(fileName, dlg.File.OpenRead());
}

接著,利用 WebClient 來上傳檔案
private void UploadFile(string fileName, Stream data)
{
    UriBuilder ub = new UriBuilder("http://localhost:22792/FileUpload/receiver.ashx");
    ub.Query = string.Format("filename={0}", fileName);
    WebClient c = new WebClient();
    c.OpenWriteCompleted += (sender, e) =>
    {
        PushData(data, e.Result); // 將 data 複製到 e.Result
        e.Result.Close();
        data.Close();
    };
    c.OpenWriteAsync(ub.Uri);
}

[reference]
http://silverlight.net/learn/learnvideo.aspx?video=69793

沒有留言:

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...