0

ファイルのように、メモリストリームをhtmlに送信することは可能ですか?

アイデアは、ハード ドライブにファイルを作成することではありません。

ストリームを作成しましたが、問題なくファイルをダウンロードできます。問題は変数に渡されて aspx ページに送信されるため、それを使用して特定の div にファイルを表示できます。

出来ますか?ディスクにファイルを作成せずに。

よろしくお願いいたします。

これは私が持っているコードです:

        public string ExportMemoryPdf(DataTable dtpdf, String Path)
    {
        string User = System.Web.HttpContext.Current.User.Identity.Name.ToString();
        string Date = DateTime.Now.ToString();
        string Year = DateTime.Now.ToString("yyyy");
        string Month = DateTime.Now.ToString("MM");
        string Day = DateTime.Now.ToString("dd");
        string Hour = DateTime.Now.ToString("hh");
        string Minutes = DateTime.Now.ToString("mm");
        string Seconds = DateTime.Now.ToString("ss");
        string FileName = User + Day + Month + Year + Hour + Minutes + Seconds;


        //Session["WhereIsIt"].ToString()
        //-----------------------Chamada de Classe de Registo de Eventos--------------------------
        //string Message = "The User Asked For a PDF File From the Table" + Request.QueryString["Position"] + "With the Filename: " + FileName;
        string Message = "The User Asked For a PDF File From the Table With the Filename: " + FileName;
        //OneGrid.ExportSettings.IgnorePaging = true;
        //OneGrid.Rebind();
        //RegisterFile.AddRegistry(User, Message, "Message");

        //------------------------------ Variaveis para aceder a Tabela --------------------------

        int columncount = dtpdf.Columns.Count;
        int rowcount = dtpdf.Rows.Count;

        //-------------------------------Iniciaçao de criação do documento -----------------------
        Document pdf1 = new Document(PageSize.A4_LANDSCAPE.Rotate());


        using (MemoryStream output = new MemoryStream())
        {

            pdf1.SetMargins(0, 0, 80, 50);
            iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 10);

            //----------------------------------Preparação da Tabela ---------------------------------
            PdfPTable table = new PdfPTable(columncount);

            //-----------------------------------Criação de Ficheiro ---------------------------------

            string path = System.Web.HttpContext.Current.Server.MapPath(Path);
            //string path = System.IO.Path.GetTempPath();
            //Label1.Text = path.ToString();
            //PdfWriter pdfWriter = PdfWriter.GetInstance(pdf1, new FileStream(path + "/" + FileName + ".pdf", FileMode.Create));

            PdfWriter pdfWriter = PdfWriter.GetInstance(pdf1, output);

            //----------------------------------- Dimensões da Tabela ---------------------------------------
            table.WidthPercentage = 90;

            //-------------------------------Criação do Header e Footer de cada folha-------------------------
            KIOSK.Classes.Header_Footer page = new Classes.Header_Footer();


            //-----------------------------------Inserção de conteudos -------------------------------
            pdfWriter.PageEvent = page;
            pdf1.Open();

            //table.AddCell(HttpContext.Current.Request.QueryString["position"].ToString());

            for (int z = 0; z < columncount; z++)
            {
                var sabersenao = dtpdf.Columns[z].ToString();
                table.AddCell(new Phrase(sabersenao, font20));
            }

            for (int u = 0; u < rowcount; u++)
            {
                int contador = 0;
                while (contador < columncount)
                {
                    var CamposCorrigidos = dtpdf.Rows[u].ItemArray[contador].ToString();

                    StringBuilder ConvPassword = new StringBuilder(CamposCorrigidos);
                    ConvPassword.Replace("&amp;", string.Empty);
                    ConvPassword.Replace("&nbsp;", string.Empty);

                    string CamposCorrigidos2 = ConvPassword.ToString();

                    table.AddCell(new Phrase(CamposCorrigidos2, font20));
                    contador += 1;
                }
            }

            //----------------------Abertura/Fecho e inserção do componentes necessrios ao pdf---------

            pdf1.Add(table);
            pdf1.Close();

            //System.Web.HttpContext.Current.Response.ClearContent();
            //System.Web.HttpContext.Current.Response.ClearHeaders();
            //System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
            //System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);

            //System.Web.HttpContext.Current.Response.BinaryWrite(output.ToArray());
            //System.Web.HttpContext.Current.Response.End();
            //System.Web.HttpContext.Current.Response.Flush();
            //System.Web.HttpContext.Current.Response.Clear();

            //System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
            //System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "PdfViewer; filename=" + FileName +".PDF");
            ////System.Web.HttpContext.Current.Response.AddHeader("content-length", output.Length.ToString());
            //System.Web.HttpContext.Current.Response.BinaryWrite(output.ToArray());

            //System.Web.HttpContext.Current.Response.End();


            //output.Read(pdfByte, 0, (int)pdfByte.Length);



            output.Read(pdfByte, 0, (int)pdfByte.Length); 

            var strBase64 = Convert.ToBase64String(pdfByte);
        }
        return Convert.ToBase64String(pdfByte);


    }

そしてエラー:

閉じたストリームにアクセスできません。

感謝

4

2 に答える 2

0

メモリ ストリームが返すコンテンツ タイプがわかっている場合は、そのタイプを処理する変数にストリームを読み取って処理できます。

文字列の例については、MemoryStream から文字列を取得する方法を参照してください。. 文字列をメモリストリームに書き込んでから、もう一度読み戻します。

于 2014-03-13T10:46:03.683 に答える
0

メモリストリームをバイト配列に変換する必要がある可能性があります。次に、バイト配列を Base64 文字列に変換する必要があります。そして最後に、そのbase64文字列をimg srcに入れる必要があります

htmlの画像へのbase64についての解説です。

これは画像に対して機能することに注意してください。これらのファイルはダウンロードする必要があり、物理的な場所が必要なため、他のファイルは機能しません。表示するファイルタイプの JavaScript ハンドラーを作成する予定がない限り。

更新: PDF の場合、次の操作を実行できますが、クロスブラウザーとの互換性がない可能性があります。

コードビハインド

//string filepath = Server.MapPath("/Temp.pdf");
byte[] pdfByte; //= Helper.GetBytesFromFile(filepath);
using(var stream = ....) {
stream.read(pdfByte,0,(int)pdfByte.length);
var strBase64=Convert.ToBase64String(pdfByte);

HTML

<object data=",<<yourBase64StringHereWithoutthesmallerthenbiggerthenquotes==>>" type="application/pdf" width="800px"></object>

コードを更新しました:

public string ExportMemoryPdf(DataTable dtpdf, String Path)
{
    string User = System.Web.HttpContext.Current.User.Identity.Name.ToString();
    string Date = DateTime.Now.ToString();
    string Year = DateTime.Now.ToString("yyyy");
    string Month = DateTime.Now.ToString("MM");
    string Day = DateTime.Now.ToString("dd");
    string Hour = DateTime.Now.ToString("hh");
    string Minutes = DateTime.Now.ToString("mm");
    string Seconds = DateTime.Now.ToString("ss");
    string FileName = User + Day + Month + Year + Hour + Minutes + Seconds;
    Request.QueryString["Position"] + "With the Filename: " + FileName;
    string Message = "The User Asked For a PDF File From the Table With the Filename: " + FileName;

    int columncount = dtpdf.Columns.Count;
    int rowcount = dtpdf.Rows.Count;

    Document pdf1 = new Document(PageSize.A4_LANDSCAPE.Rotate());
        pdf1.SetMargins(0, 0, 80, 50);
        iTextSharp.text.Font font20 = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 10);
        PdfPTable table = new PdfPTable(columncount);
        string path = System.Web.HttpContext.Current.Server.MapPath(Path);
        table.WidthPercentage = 90;
        KIOSK.Classes.Header_Footer page = new Classes.Header_Footer();
        for (int z = 0; z < columncount; z++)
        {
            var sabersenao = dtpdf.Columns[z].ToString();
            table.AddCell(new Phrase(sabersenao, font20));
        }
        for (int u = 0; u < rowcount; u++)
        {
            int contador = 0;
            while (contador < columncount)
            {
                var CamposCorrigidos = dtpdf.Rows[u].ItemArray[contador].ToString();

                StringBuilder ConvPassword = new StringBuilder(CamposCorrigidos);
                ConvPassword.Replace("&amp;", string.Empty);
                ConvPassword.Replace("&nbsp;", string.Empty);

                string CamposCorrigidos2 = ConvPassword.ToString();

                table.AddCell(new Phrase(CamposCorrigidos2, font20));
                contador += 1;
            }
        }
    var base64String = string.Empty;
    using (MemoryStream output = new MemoryStream())
    {
        PdfWriter pdfWriter = PdfWriter.GetInstance(pdf1, output);
        pdfWriter.PageEvent = page;
        pdf1.Open();
        pdf1.Add(table);
        pdf1.Close();

        bytes = output.ToArray();

        var base64String = Convert.ToBase64String(bytes);
    }
    return base64String;


}
于 2014-03-13T10:46:40.577 に答える