無法從數據庫加載圖像

[英]Failed to load Image from database


I have a file upload and download using ajaxFileUploader and 2 handlers.

我使用ajaxFileUploader和2個處理程序上傳和下載文件。

ajaxFileUploader code:

    $.ajaxFileUpload
    (
        {
            url: 'AjaxFileUploader.ashx?user=' + userId,
            secureuri: false,
            fileElementId: 'uploadControl',
            dataType: 'json',
            data: '{}',
            success: function (mydata) {

                alert("Image Successfully Uploaded");

                $('#imgdefaultphoto').attr('src', 'ImageRetrieval.ashx?user=' + userId);
            },
            error: function () {

            }
        }
    )

AjaxFileUploader.ashx code:

    public void ProcessRequest(HttpContext context)
    {

        if (context.Request.Files.Count > 0)
        {
            string path = context.Server.MapPath("~/Temp");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            var file = context.Request.Files[0];

            string userid = context.Request.QueryString["user"];

            string fileName;

            if (HttpContext.Current.Request.Browser.Browser.ToUpper() == "IE")
            {
                string[] files = file.FileName.Split(new char[] { '\\' });
                fileName = files[files.Length - 1];
            }
            else
            {
                fileName = file.FileName;
            }
            string fileType = file.ContentType;
            string strFileName = fileName;

            int filelength = file.ContentLength;
            byte[] imagebytes = new byte[filelength];
            file.InputStream.Read(imagebytes, 0, filelength);

            DBAccess dbacc = new DBAccess();
            dbacc.saveImage(imagebytes, userid);

            var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            var result = new { name = file.FileName };
            context.Response.Write(serializer.Serialize(result));

        }

ImageRetrieval.ashx code:

     public void ProcessRequest(HttpContext context)
    {

        string userId = HttpContext.Current.Request.QueryString["user"];

        if (userId != null)
        {
            DBAccess dbacc = new DBAccess();

            DataTable dt = dbacc.getImage(userId);

            context.Response.ContentType = "image/png";

            context.Response.BinaryWrite((byte[])dt.Rows[0]["UserImage"]);

            context.Response.Flush();
        }
        else
        {
            context.Response.Write("No Image Found");
        }
    }

The image is being upload at the database but when I try to load it and append it to my img tag, the image tag shows broken Image. I don't know what causes or seems to be the problem. Any help will be appreciated. Thanks!

圖像正在數據庫上傳,但是當我嘗試加載它並將其附加到我的img標簽時,圖像標簽會顯示損壞的圖像。我不知道是什么原因或似乎是問題。任何幫助將不勝感激。謝謝!

Edited some lines. Still no luck.

編輯了一些台詞。仍然沒有運氣。

1 个解决方案

#1


0  

BAsed on your last remark try this:

在你上一次評論的基礎上試試這個:

var img = dt.Rows[0]["UserImage"];
byte[] imagebytes = img.ToArray();
context.Response.BinaryWrite(imagebytes);

or

static byte[] GetBytes(string str)
{
    byte[] bytes = new byte[str.Length * sizeof(char)];
    System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
    return bytes;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/07/11/29b18c41460e27ade7ef654081f81e19.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com