2011年1月29日 星期六

[Silverlight] Drag to here !!

總算也把這個東西做出來了,多虧Silverlight4.0 的拖曳功能,以及Code pj 上老外朋友提供的範例
說起來之前一直看不起Silverlight,因為MS的Selas整天只會說清明上河圖
SL 4.0出來感覺滿有劃世代的感覺呢 !




0.宣告物件
        public FileInfo File
        {
            get { return _file; }
            set
            {
                _file = value;
                Stream temp = _file.OpenRead();
                FileLength = temp.Length;
                temp.Close();
                BytesUploaded = 0;
            }
        }
1.觸發
拉一個image物件,然後設定其
Allow drop = true
Drop事件:
private void image1_Drop(object sender, DragEventArgs e)
        {
            IDataObject dataObject = e.Data;
            FileInfo[] fileInfoArray = dataObject.GetData("FileDrop") as FileInfo[];
            e.Handled = true;
        }
2.取得檔案
private void SendNextFile()
        {
            if (_listFiles.Count > 0)
            {
                File = _listFiles[0];

                //檔案大小檢查
                long fz = Convert.ToInt32(file_Size) * 1024 * 1024;
                if (fz > 0) ChunkSize = fz;
                if (File.Length > fz) { MessageBox.Show("檔案大小限制:" + file_Size "MB"); return; }

                //上傳                
                UploadFileEx();
            }
3.將資料post到Server
 private void UploadFileEx()
        {
            long temp = FileLength - BytesUploaded;
            string ubi = HOST_URL + "/" + ASHX_URL; //處理檔案的對應泛型處理常式
            UriBuilder ub = new UriBuilder(ubi);
            string new_id = Guid.NewGuid().ToString();
            ub.Query = string.Format("{4}filename={0}&StartByte={1}&Complete={2}&newid={3}", File.Name, BytesUploaded, complete, new_id, string.IsNullOrEmpty(ub.Query) ? "" : ub.Query.Remove(0, 1) + "&");
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(ub.Uri);
            webrequest.Method = "POST";
            IAsyncResult ia = webrequest.BeginGetRequestStream(new AsyncCallback(WriteCallback), webrequest);
            if (ia.IsCompleted)  SetLabelObj();              
                    }
4.利用委派的方式更新進度列
 public delegate void UpdateUICallBack();
        private void UpdataPrg(long uped)
        {
            lbl_bytes.Content = uped.ToString() + " / " + FileLength.ToString();
        }
        public void UpdataUICompile()
        {
        }

參數的傳入:利用"InitParams"傳入參數
<param name="InitParams" value="fileSize=4, ... >

結果的輸出:利用Silverlight調用Javascript
HtmlPage.Window.Invoke("RunEndScript", v);


--
看到、想到、說到、做到 
能夠填平大海的誓言,也比不上邁出一步的價值 
是以吾輩此生,再無任何誓言

沒有留言: