2016-04-03 81 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

相关问题