0

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;
}
4

1 に答える 1

4

SaveFileDialogWeb サーバーからローカル コンピューターを使用して [ファイルの保存] ダイアログを表示することはできません。ブラウザーを介してファイルの保存ダイアログをトリガーできる唯一の方法は、ユーザーを 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 ハンドラー ASP.NET サンプル リクエスト

于 2013-04-29T12:42:57.603 に答える