2016-04-03 80 views
1

我現在面臨的問題是,當我嘗試上傳byte[]Azure的 Blob存儲我收到以下異常:型「System.Web.HttpInputStream」未標記爲可序列

Error: Type 'System.Web.HttpInputStream' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

我因此去了將代碼所在的類標記爲[Serializable],但仍引發相同的異常。

Upload.aspx.cs:

[Serializable] 
    public partial class Upload : System.Web.UI.Page 
    { 
     protected void submitButton_Click(object sender, EventArgs args) 
     { 
      HttpPostedFile filePosted = Request.Files["File1"]; 
      string fn = Path.GetFileName(filePosted.FileName); 
      try 
      {     
       byte[] bytes = ObjectToByteArray(filePosted.InputStream); 
       Share[] shares = f.split(bytes); 
       UploadImageServiceClient client = new UploadImageServiceClient(); 
       client.Open(); 
       foreach (Share share in shares) 
       { 
        byte[] serialized = share.serialize(); 
        Response.Write("Processing upload..."); 
        client.UploadImage(serialized); 
       } 
       client.Close(); 
      } 
      catch (Exception ex) 
      { 
       Response.Write("Error: " + ex.Message); 
      } 
     } 
} 

我知道有諸如this類似的問題,其解釋說,你不能定義一個數據合同與流成員,但我的WCF的雲服務不設流或FileStream成員。

這裏是我的WCF服務的實現:

[ServiceContract] 
public interface IUploadImageService 
{ 
    [OperationContract] 
    void UploadImage(byte[] bytes); 
} 

我的服務如下:

public void UploadImage(byte[] bytes) 
{ 
    // Retrieve storage account from connection string. 
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
     CloudConfigurationManager.GetSetting(connString)); 
    // Create the blob client. 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
    // Retrieve reference to a previously created container. 
    CloudBlobContainer container = blobClient.GetContainerReference("test"); 
    // Retrieve reference to a blob passed in as argument. 
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("sample"); 
    container.CreateIfNotExists(); 
    try 
    { 
     blockBlob.UploadFromByteArray(bytes, 0, bytes.Length); 
    } 
    catch (StorageException ex) 
    { 
     ex.ToString(); 
    } 
} 

回答

1

您正在試圖序列整個流對象的位置:

byte[] bytes = ObjectToByteArray(filePosted.InputStream);

你應該只是複製出來的字節流入byte[]並提交。

下面是一個使用內存流一個簡單的例子:

 byte[] bytes; // you'll upload this byte array after you populate it. 
     HttpPostedFile file = Request.Files["File1"]; 
     using (var mS = new MemoryStream()) 
     { 
      file.InputStream.CopyTo(mS); 
      bytes = mS.ToArray(); 
     } 
+0

感謝我不得不改變一些其他的事情,才能正是我想要的,但你的答案沒有消除我收到異常 – smoggers

相關問題