如何从 silverlight 文件夹中打开文件
How to open a file from silverlight folder
我的项目(Silverlight 端)的文件夹(Common)中有一个 xlsx 文件 (New.xlsx)。
我想在按钮单击事件中访问该文件路径并想打开该文件。
我使用了以下路径:
string path = @"/Common/New.xlsx";
string path1 = excel.Workbooks.Open(path);
excel.Visible = true;
但它不起作用,我无法打开该文件。
如何在 Silverlight 中使用文件路径访问文件?
尝试以下操作:
var TemplateUri = new Uri("/Common/New.xlsx, UriKind.Relative");
var stream = Application.GetResourceStream(sheetUri).Stream;
该文件部署在 xap 文件(它是一个 zip 文件)中,不能由磁盘上的普通文件处理。
我不清楚你使用的 Excel 库是什么,但它应该允许你从 Stream
加载数据。
您可以通过几个选项来授予对相关文件的访问权限。
- 您可以获取文件的内容作为流,然后通过
SaveFileDialog
class 要求用户保存文件。然后用户必须 select 他们想要保存文件的位置,然后手动打开它。
public static byte[] GetBytesFromStream(Stream input){
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream()){
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0){
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public void OnButtonClick(){
var templateUri = new Uri("/Common/New.xlsx, UriKind.Relative");
var templateStream = Application.GetResourceStream(templateUri).Stream;
var bytes = GetBytesFromStream(templateStream);
var sfd = new SaveFileDialog() {
DefaultExt = "xlsx",
Filter = "Excel Files (*.xlsx)|*.xlsx|All files(*.*)|*.*",
FilterIndex = 1
};
if (sfd.ShowDialog() == true) {
using (Stream stream = sfd.OpenFile()) {
stream.Write(bytes, 0, bytes.Length);
}
}
}
- 您可以将文件存储在服务器端,当用户单击按钮时,您告诉浏览器获取相关文件。然后浏览器将接管并询问用户是否要将文件保存到磁盘或使用已知应用程序打开。
public void OnButtonClick(){
string link = "{the url/endpoint to the file on the server}";
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(link), "_blank");
}
- 您可以走
AutomationFactory
路线,但这需要像建议的那样进行大量配置更改 here
我认为将这些东西放在服务器端比放在客户端要好得多。服务器更适合处理此类处理。
我的项目(Silverlight 端)的文件夹(Common)中有一个 xlsx 文件 (New.xlsx)。
我想在按钮单击事件中访问该文件路径并想打开该文件。
我使用了以下路径:
string path = @"/Common/New.xlsx";
string path1 = excel.Workbooks.Open(path);
excel.Visible = true;
但它不起作用,我无法打开该文件。
如何在 Silverlight 中使用文件路径访问文件?
尝试以下操作:
var TemplateUri = new Uri("/Common/New.xlsx, UriKind.Relative");
var stream = Application.GetResourceStream(sheetUri).Stream;
该文件部署在 xap 文件(它是一个 zip 文件)中,不能由磁盘上的普通文件处理。
我不清楚你使用的 Excel 库是什么,但它应该允许你从 Stream
加载数据。
您可以通过几个选项来授予对相关文件的访问权限。
- 您可以获取文件的内容作为流,然后通过
SaveFileDialog
class 要求用户保存文件。然后用户必须 select 他们想要保存文件的位置,然后手动打开它。
public static byte[] GetBytesFromStream(Stream input){
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream()){
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0){
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public void OnButtonClick(){
var templateUri = new Uri("/Common/New.xlsx, UriKind.Relative");
var templateStream = Application.GetResourceStream(templateUri).Stream;
var bytes = GetBytesFromStream(templateStream);
var sfd = new SaveFileDialog() {
DefaultExt = "xlsx",
Filter = "Excel Files (*.xlsx)|*.xlsx|All files(*.*)|*.*",
FilterIndex = 1
};
if (sfd.ShowDialog() == true) {
using (Stream stream = sfd.OpenFile()) {
stream.Write(bytes, 0, bytes.Length);
}
}
}
- 您可以将文件存储在服务器端,当用户单击按钮时,您告诉浏览器获取相关文件。然后浏览器将接管并询问用户是否要将文件保存到磁盘或使用已知应用程序打开。
public void OnButtonClick(){
string link = "{the url/endpoint to the file on the server}";
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(link), "_blank");
}
- 您可以走
AutomationFactory
路线,但这需要像建议的那样进行大量配置更改 here
我认为将这些东西放在服务器端比放在客户端要好得多。服务器更适合处理此类处理。