gpt4 book ai didi

c# - 类型 'System.Web.HttpInputStream' 未标记为可序列化

转载 作者:行者123 更新时间:2023-11-30 23:27:45 24 4
gpt4 key购买 nike

我面临的问题是,当我尝试将 byte[] 上传到 Azure blob 存储时,我收到以下异常:

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

因此,我开始使用 [Serializable] 标记代码所在的类,但仍然引发了相同的异常。

上传.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这说明您无法定义与 Stream 成员的数据协定,但我的 WCF 云服务不具有 Stream 或 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();
}
}

最佳答案

您正在尝试在此处序列化整个流对象:

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();
}

关于c# - 类型 'System.Web.HttpInputStream' 未标记为可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36389594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com