2012-07-12 280 views
0

我正在處理從數據庫顯示的此圖像。我將字節數組從數據庫轉換爲圖像顯示在我的圖像標籤上。從數據庫中顯示圖像

這裏是我上傳的圖片:

$("#uploadBtn").live("click", function() { 

$("#uploading") 
    .ajaxStart(function() { 
     $(this).show(); 
    }) 
    .ajaxComplete(function() { 
     $(this).hide(); 
    }); 

$.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); //referencing the ImageRetrieval handler 

       hideUploadMask(); 
      }, 
      error: function() { 

      } 
     } 
    ) 
    }); 

我有一個例外:

Argument exception Parameter is not valid. at the line with (*). 

ImageRetrieval.ashx

public void ProcessRequest(HttpContext context) 
    { 
     string userid = context.Request.QueryString["user"]; 
     DBAccess dbacc = new DBAccess(); 
     DataTable dt = dbacc.getImage(userid); 
     byte[] image = ((byte[])dt.Rows[0]["UserImage"]); 
     System.Drawing.Image img = byteArrayToImage(image); 

     MemoryStream stream = new MemoryStream(); 
     img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); 
     img.Dispose(); 
     stream.Position = 0; 
     byte[] data = new byte[stream.Length]; 
     stream.Read(data, 0, (int)stream.Length); 
     stream.Dispose(); 
     context.Response.Clear(); 
     context.Response.ContentType = "image/jpeg"; 
     context.Response.BinaryWrite(data); 
    } 

    public Image byteArrayToImage(byte[] byteArrayIn) 
    { 
     MemoryStream ms = new MemoryStream(byteArrayIn); 
     Image returnImage = Image.FromStream(ms); // exception line (*) 
     return returnImage; 
    } 

任何想法?

+0

您是否嘗試過自定義觸發選項選項? – TheVillageIdiot 2012-07-12 16:22:44

+0

不,我不知道這一點。你有樣品嗎? – ljpv14 2012-07-12 16:23:16

回答

0

根據此處的文檔:http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx,如果提供的流爲null(即檢查代碼,將不會)或不包含有效圖像,則Image.FromStream將拋出參數異常。我懷疑後者是這種情況。

這是我想嘗試:你在你的代碼的字節數組。只需在後:

byte[] image = ((byte[])dt.Rows[0]["UserImage"]); 

圖像數據寫入到本地文件,並檢查它 - 我懷疑你會發現它不是不是一個圖像。如果是這種情況,那麼你的問題就變成「爲什麼不是有效的圖像上傳?」。