簡體   English   中英

SaveFileDialog不起作用

[英]SaveFileDialog doesn't work

我正在制定一個創建excel文件的操作,然后打開一個SaveFileDialog,允許客戶端將其保存在他們的計算機中。

問題是在Local中,SaveFileDialog可以工作,但是出現在我的瀏覽器窗口后面,這是一個問題...

然后,當我在服務器中發布它時,SaveFileDialog完全不起作用。 我花了大約2天的時間在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;
}

從Web服務器到本地計算機,將無法使用SaveFileDialog顯示“保存文件”對話框。 通過瀏覽器觸發“保存文件”對話框的唯一方法是將用戶鏈接到Web服務器上的文件,或者提供具有正確MIME類型的頁面。 通常,這是使用HTTP處理程序完成的。

服務器本身將打開“保存文件”對話框。

例:

<%@ 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示例請求

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM