简体   繁体   English

如何在SQL Server中将存储的pdf btye数组显示到图像框中的asp.net WebForm?

[英]How to show stored pdf btye array in SQL Server to asp.net WebForm in a image box?

i am developing a web application in that i need to store image files and some other documents in a database and then i have to show them on a web page in an image box preferably. 我正在开发一个Web应用程序,我需要将图像文件和一些其他文档存储在数据库中,然后我必须在图像框中的网页上显示它们。 i have stored images in database and also PDF files in the form of byte array and i used a Generic Handler to get byte array from database and then show them in a image box it works fine for the images but it does not work with PDF files. 我已经在数据库中存储了图像,也以字节数组的形式存储了PDF文件,我使用Generic Handler从数据库中获取字节数组,然后在图像框中显示它对图像工作正常,但它不适用于PDF文件。

public class ShowImage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    { 
        Int32 imgID;
        if (context.Request.QueryString["id"] != null)
            imgID = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        byte[] buffer = ShowEmpImage(imgID);
        context.Response.BinaryWrite(buffer);
    }

    public Stream ShowEmpImage(int empno)
    {
        dbClass db = new dbClass();
        string sql = "SELECT imgfile FROM myFiles WHERE id = @ID";
        SqlCommand cmd = new SqlCommand(sql, db.con);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", empno);
        object img = null;
        try
        {
            db.Connect();
            img = cmd.ExecuteScalar();
        }
        catch
        {
            return null;
        }
        finally
        {
            db.Disconnect();
        }
        return new MemoryStream((byte[])img);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

please guide me how can i show PDF files and some other files to the image box? 请指导我如何在图像框中显示PDF文件和其他一些文件? actually the problem is my application runs with the existing application and that have no way to store extensions of files so the only way i can show files is "show them in as a image" but if their is a better way then help me out friends. 实际问题是我的应用程序运行与现有的应用程序,并没有办法存储文件的扩展,所以我可以显示文件的唯一方法是“显示他们作为一个图像”,但如果他们是一个更好的方式然后帮助我的朋友。

thanks @Marc Gravell when i choose file and click on a button to upload i goes a page like visual studio is closed and i want to run my project. 感谢@Marc Gravell,当我选择文件并单击按钮上传时我会关闭像visual studio这样的页面,我想运行我的项目。 some thing like that. 有点像这样。 here is my code that i am using to save PDF and image files. 这是我用于保存PDF和图像文件的代码。

string sql = "INSERT INTO uploadedfiles(name, srID, policy, date, LOGpdf, fileExtension) VALUES(@p, @d, @pol, @dt, @img, @extension) SELECT @@IDENTITY";

                db.Connect();
                SqlCommand cmd = new SqlCommand(sql, db.con);
                cmd.Parameters.AddWithValue("@p", txtName.Text);
                cmd.Parameters.AddWithValue("@d", txtSrID);
                cmd.Parameters.AddWithValue("@pol", lblPolicyID.Text);
                cmd.Parameters.AddWithValue("@dt", txtDate.Text);
                if (FileUpload1.HasFile)
                {
                    cmd.Parameters.AddWithValue("@img", imgByte);
                    cmd.Parameters.AddWithValue("@extension", extension);
                }
                int id = Convert.ToInt32(cmd.ExecuteScalar());

i was searching and discussing with my friend and we come up with this, code and this help me but my friend said that it will only upload files upto 100MB. 我正在和我的朋友一起搜索和讨论,我们想出了这个代码,这对我很有帮助,但我的朋友说它只会上传高达100MB的文件。

<location path="UploadFiles.aspx">
    <system.web>
        <httpRuntime executionTimeout="2040" maxRequestLength="104857600"/>
    </system.web>
</location>

And in my application might be user want to upload files having size more than 100MB in that case this will also crash. 在我的应用程序中,用户可能希望上传大小超过100MB的文件,在这种情况下,这也会崩溃。

There's a reason that PDF doesn't work when representing it as an image... it isn't an image . 当将PDF表示为图像时,PDF不起作用的原因...... 它不是图像 Additionally, claiming it is "image/jpeg" is going to make it pretty broken unless the browser does explicit file-header checking, and really need fixing IMO. 此外,声称它是"image/jpeg"将使它非常破碎,除非浏览器进行显式的文件头检查,并且确实需要修复IMO。 If you don't currently store the file name / extension / MIME / something then frankly: that's a problem you need to fix. 如果你当前没有存储文件名/ extension / MIME /那么坦率地说:那是你需要解决的问题。

For displaying a PDF, I suspect you'd need to use an <iframe> rather than an <img> , which a: might not work on all browsers, and b: might not be very convenient to the caller. 为了显示PDF,我怀疑你需要使用<iframe>而不是<img> ,a:可能不适用于所有浏览器,而b:对调用者来说可能不太方便。 Personally I'd just offer a download link (and in the handler, use content-disposition to tell the browser to treat it as a download). 就个人而言,我只提供一个下载链接(在处理程序中,使用content-disposition告诉浏览器将其视为下载)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM