我正在开发一个动作,它创建一个excel文件,然后打开一个SaveFileDialog,允许客户将其保存在他们的计算机中。
问题是,在本地,SaveFileDialog可以工作,但出现在我的浏览器窗口后面,这是一个问题……
然后,当我在我的服务器上发布它时,SaveFileDialog完全不起作用。大概两天我在Stackoverflow上阅读了一些关于这个的其他话题,但我仍然没有找到正确的答案……
(很抱歉我的英语错误,我是法国人)。下面是我的代码:
String path = string.Empty;
object misValue = System.Reflection.Missing.Value;
bool canExport = false;
Excel.Application app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Add(1);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
Excel.Range rng = (Excel.Range)ws.get_Range("A1", "K1");
/** Excel File completion **/
/** ... **/
/** ... **/
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = Environment.SpecialFolder.Personal.ToString();
sfd.Filter = "Classeur Excel 2010 (*.xls)|*.xls";
if (STAShowDialog(sfd) == DialogResult.OK)
{
path = sfd.FileName;
if (System.IO.File.Exists(path))
{
try
{
System.IO.File.Delete(path);
canExport = true;
}
catch
{
MessageBox.Show("Impossible d'écrire par-dessus ce fichier.");
canExport = false;
}
}
else
{
canExport = true;
}
}
SaveExcelFile(canExport, wb, path, app, ws, misValue);
return View();现在来看我的函数:
public void SaveExcelFile(bool canExport, Excel.Workbook wb, String path, Excel.Application app, Excel.Worksheet ws, object misValue)
{
if (canExport)
{
try
{
wb.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
wb.Close(true, misValue, misValue);
app.Quit();
}
catch
{
MessageBox.Show("Problème durant l'exportation\r\n Code erreur #EX01");
}
}
releaseObject(ws);
releaseObject(wb);
releaseObject(app);
}
private DialogResult STAShowDialog(FileDialog dialog)
{
DialogState state = new DialogState();
state.dialog = dialog;
System.Threading.Thread t = new System.Threading.Thread(state.ThreadProcShowDialog) { IsBackground = true, Name = "threadExport", Priority = System.Threading.ThreadPriority.AboveNormal };
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
return state.result;
}发布于 2013-04-29 20:42:58
您将无法使用SaveFileDialog将文件保存对话框从web服务器显示到本地计算机。通过浏览器触发“保存文件”对话框的唯一方法是将用户链接到Web服务器上的文件,或者使用正确的MIME类型提供页面。这通常是使用HTTP Handler来完成的。
服务器上正在打开保存文件对话框...
示例:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
HttpResponse r = context.Response;
r.ContentType = "image/png";
//
// Write the requested image
//
string file = context.Request.QueryString["file"];
// Get Data From Database. And write file.
if (file == "logo")
{
r.WriteFile("Logo1.png");
}
else
{
r.WriteFile("Flower1.png");
}
}
public bool IsReusable {
get {
return false;
}
}
}来源:Http Handler ASP.NET Sample Request
https://stackoverflow.com/questions/16278711
复制相似问题