閱讀: 84 評論: 0 做者: blackcore 發表於 2009-11-21 22:47 原文連接html
通常來講,文件上傳老是須要的,能夠經過ashx及其wcf或其它方式實現,這裏主要是wcf實現方式,並附之簡單的進度顯示。。。
1. silverlight 項目通常有silverlight和silverlight.web(asp.net)兩個基本項目,在這裏咱們須要在silverlight.web(asp.net)項目中添加一個Silverlight enabled wcf service文件,其功能主要是實現文件上傳。
WCF文件所在項目:
web
WCF文件類型:
windows
相應代碼以下:asp.net
Code
namespace BlackCore.Web
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}
// Add more operations here and mark them with [OperationContract]
[OperationContract]
public void ActionUpload(string fileName, byte[] fileContext, bool isAppend)
{
//文件上傳所在目錄
//能夠按日期、文件類型或混合創建相應的文件目錄,以便使用
string uploadFolder = System.Web.Hosting.HostingEnvironment.MapPath("~/upload");
//目錄不存在則新建
if (!System.IO.Directory.Exists(uploadFolder))
{
System.IO.Directory.CreateDirectory(uploadFolder);
}
//文件讀寫模式
System.IO.FileMode fileMode = System.IO.FileMode.Create;
//若是參數爲真則表示續傳,以追回模式寫文件
if (isAppend)
{
fileMode = System.IO.FileMode.Append;
}
//寫入文件
using (System.IO.FileStream fs = new System.IO.FileStream(uploadFolder + @"\" + fileName, fileMode, System.IO.FileAccess.Write))
{
fs.Write(fileContext, 0, fileContext.Length);
}
return;
}
}
}
2. 在Silverlight中添加服務引用就OK,而後在相應的界面實現便可,簡單實現以下:網站
在MainPage.xaml加入了以下一個Buttonui
<
Button
Grid.Row
="1"
Grid.Column
="3"
x:Name
="btnWCFUpload"
Content
="WCFUpload"
Height
="20"
Width
="80"
/>
在MainPage.xaml.cs中加入以下this
Code
public MainPage()
{
this.btnWCFUpload.Click += new RoutedEventHandler(btnWCFUpload_Click);
}
///
/// WCF 文件上傳
///
///
///
void btnWCFUpload_Click(object sender, RoutedEventArgs e)
{
//選擇本地文件對話框
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "(*.*)|*.*";
//單選
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == true)
{
//所選上傳文件信息
FileInfo fileInfo = openFileDialog.File;
//上傳文件信息
UploadFileInfo uploadFile = new UploadFileInfo();
uploadFile.Name = fileInfo.Name;
//文件內容
Stream stream = fileInfo.OpenRead();
uploadFile.Size = stream.Length / 1024.00;
uploadFile.Context = new List<byte[]>();
//讀取指定大小的文件流內容到uploadFile.Context以便上傳
int b;
while (stream.Position > -1 && stream.Position < stream.Length)
{
if (stream.Length - stream.Position >= 3000)
{
b = 3000;
}
else
{
b = (int)(stream.Length - stream.Position);
}
byte[] filebyte = new byte[b];
stream.Read(filebyte, 0, b);
uploadFile.Context.Add(filebyte);
}
stream.Close();
UploadService.Service1Client wcfService = new BlackCore.UploadService.Service1Client();
wcfService.ActionUploadCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(wcfService_ActionUploadCompleted);
wcfService.ActionUploadAsync(uploadFile.Name, uploadFile.Context[0], false, uploadFile);
}
}
void wcfService_ActionUploadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
UploadFileInfo uploadFile = e.UserState as UploadFileInfo;
uploadFile.CompletedStatus += uploadFile.Context[0].Length / 1024.00;
uploadFile.Context.RemoveAt(0);
if (uploadFile.Context.Count == 0)
{
btnWCFUpload.Content = "WCFUpload";
MessageBox.Show("文件上傳成功!");
}
else
{
(sender as UploadService.Service1Client).ActionUploadAsync(uploadFile.Name, uploadFile.Context[0], true, uploadFile);
btnWCFUpload.Content = uploadFile.CompletedStatus.ToString() + "/" + uploadFile.Size.ToString() + "KByte";
}
}
}
///
/// 當前上傳文件信息
///
public class UploadFileInfo
{
///
/// 文件名
///
public string Name { get; set; }
///
/// 文件大小
///
public double Size { get; set; }
///
/// 上傳完成狀態
///
public double CompletedStatus { get; set; }
///
/// 文件流
///
public List<byte[]> Context { get; set; }
}
3. 效果以下spa


固然,若是用wcf實現方式,可能會給發佈帶來必定麻煩。。。.net
由於在ClientBin中的BlackCore.xap(這裏個人項目的壓縮包)中的ServiceReferences.ClientConfig中有生成的配置信息,若是要部署是須要更改的
code
Servicereferences.ClientConfig文件配置信息以下:
Code
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_Service1">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
binding>
customBinding>
bindings>
<client>
<endpoint address="http://localhost:16827/UPloadWCFService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_Service1"
contract="UploadService.Service1" name="CustomBinding_Service1" />
client>
system.serviceModel>
configuration>
因此,使用WCF還應該想個辦法解決發佈部署問題,也就算WCF文件上傳是成功的。
此問題本人暫時沒有解決,如遇能人,懇請賜教,謝謝!
發表評論
新聞頻道:Google手機將全球發售 無鎖零售版530美圓
推薦連接:Windows 7專題發佈
網站導航:博客園首頁 我的主頁 新聞 社區 博問 閃存 知識庫