0

PDF サムネイルを作成するための次のコードがありますが、このコードは HttpPostedFile を使用した入力ファイルに対してのみ機能します。しかし、私のファイルはファイルシステムにあり、HttpPostedFile なしでファイルを処理する方法がわかりません。ファイル システムでファイルのサムネイルを作成するにはどうすればよいですか?

これはコードです:

<%@ Page language="c#" %> 
<%@ Register TagPrefix="tc" Assembly="TallComponents.PDFThumbnail" Namespace="TallComponents.Web.PDF" %> 
<script runat="server"> 
    protected void DisplayThumbs_Click(object sender, System.EventArgs e) 
    { 
        thumbnails.Controls.Clear(); 
        HttpPostedFile postedFile = Request.Files[ "pdf" ]; 
        if ( null != postedFile ) 
        { 
            byte[] buffer = new byte[ postedFile.ContentLength ]; 
            postedFile.InputStream.Read( buffer, 0, buffer.Length ); 
            string unique = Guid.NewGuid().ToString(); 
            Session[ unique ] = buffer;      

            int n = Thumbnail.GetPageCount( postedFile.InputStream ); 

            for ( int i=1; i<=n; i++ ) 
            { 
                Thumbnail thumbnail = new Thumbnail(); 
                thumbnail.SessionKey = unique; 
                thumbnail.Index = i; 
                thumbnails.Controls.Add( thumbnail ); 
            } 
        } 
    } 
</script> 
<HTML>
    <body> 
        <form id="Form1" method="post" runat="server" enctype="multipart/form-data"> 
            <input width="100%" type="file" size="50" name="pdf" /> <br/> 
            <asp:button Runat="server" Text="Display thumbs" ID="DisplayThumbs" OnClick="DisplayThumbs_Click" /> <br/> 
            <asp:Panel ID="thumbnails" runat="server" /> 
        </form> 
    </body> 
</HTML> 

前もって感謝します

4

2 に答える 2

1

これを使用してファイル システム ベースのストリームを処理する場合は、asp.net コードの「コア論理コード」のみが必要です。次のように使用できます。

private static IList<Thumbnail> GetThumbnails(System.IO.File pdfFile) 
{ 
    string unique = Guid.NewGuid().ToString(); 

    using(System.IO.Stream stream = pdfFile.OpenRead())
    {
        int n = Thumbnail.GetPageCount( stream ); 
        List<Thumbnail> thumbnails = new List<Thumbnail>();

        for ( int i=1; i<=n; i++ )   // Note: please confirm the loop should really start from the index 1 (instead of 0?)
        { 
            Thumbnail thumbnail = new Thumbnail(); 
            thumbnail.SessionKey = unique; 
            thumbnail.Index = i; 
            thumbnails.Add( thumbnail ); 
        }

        return thumbnails;
    }
}
于 2014-07-29T06:27:59.220 に答える
0

SaveAsHttpPostedFile クラスの関数を使用して、ファイルを保存して使用することができます。

すべてを説明するリンクは次のとおりです 。 http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.saveas(v=vs.110).aspx

于 2014-07-29T04:28:51.810 に答える