在网格中显示文件名并在silverlight中下载文件 [英] show filenames in grid and download file in silverlight
本文介绍了在网格中显示文件名并在silverlight中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Silverlight-4.0中显示数据网格中特定目录中所有文件的名称.
并下载该文件.
在此先感谢您.
I want to show the names of all the files in a specific directory in data grid in silverlight-4.0
and download that file.
Thanks in advance.
推荐答案
您可以使用
You can use
string folderPath = ""; //
string[] files = System.IO.Directory.GetFiles(folderPath);// this also accepts a pattern if you want to filter the files to a particular extension etc.
这是我最终的解决方案得到并成功工作.. :)
Here is the solution that i finally got and works successfully.. :)
//in xaml.cs
public void BtnDownload_click(object sender, EventArgs e)
{
var currentRow = dgDownloadFile.SelectedItem;
string sFileName = currentRow.ToString();
string sFullPath = "C:/FilesToDownload/" + sFileName; //file path from where you want to download file
Service1Client proxy = new Service1Client();
proxy.DownloadFileCompleted += new ventHandler<DownloadFileCompletedEventArgs>(proxy_DownloadFileCompleted);
proxy.DownloadFileAsync(sFullPath); //wcf service client
}
void proxy_DownloadFileCompleted(object sender, DownloadFileCompletedEventArgs e)
{
if (e.Result == 1)
MessageBox.Show("Download successfully.");
if(e.Result == 2)
MessageBox.Show("File already exists.");
}
//in WcfService
[WebMethod]
public int DownloadFile(string FullPath)
{
string[] fn = FullPath.Split('/'); //get your filename
FileStream fileStream = null;
BinaryReader reader = null;
fileStream = new FileStream(FullPath, FileMode.Open, FileAccess.Read);
reader = new BinaryReader(fileStream);
byte[] fileeBytes = reader.ReadBytes((int)fileStream.Length);
string DownloadPath = "C:/Documents/" + fn[2]; //for download in C:/Documents
if (!File.Exists(DownloadPath))
{
FileStream outputStream = new FileStream(DownloadPath, FileMode.Create);
outputStream.Write(fileeBytes, 0, fileeBytes.Length);
outputStream.Close();
return 1;
}
else
{
return 2;
}
}
//you need to add following files.
clientaccesspolicy.xml
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="https://*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type" />
</cross-domain-policy>
这篇关于在网格中显示文件名并在silverlight中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文