gpt4 book ai didi

file-upload - Azure存储: Uploaded files with size zero bytes

转载 作者:行者123 更新时间:2023-12-03 09:36:31 26 4
gpt4 key购买 nike

当我将图像文件上传到 blob 时,图像显然已成功上传(没有错误)。当我进入云存储工作室时,该文件就在那里,但大小为 0(零)字节。

以下是我正在使用的代码:

// These two methods belong to the ContentService class used to upload
// files in the storage.
public void SetContent(HttpPostedFileBase file, string filename, bool overwrite)
{
CloudBlobContainer blobContainer = GetContainer();
var blob = blobContainer.GetBlobReference(filename);

if (file != null)
{
blob.Properties.ContentType = file.ContentType;
blob.UploadFromStream(file.InputStream);
}
else
{
blob.Properties.ContentType = "application/octet-stream";
blob.UploadByteArray(new byte[1]);
}
}

public string UploadFile(HttpPostedFileBase file, string uploadPath)
{
if (file.ContentLength == 0)
{
return null;
}

string filename;
int indexBar = file.FileName.LastIndexOf('\\');
if (indexBar > -1)
{
filename = DateTime.UtcNow.Ticks + file.FileName.Substring(indexBar + 1);
}
else
{
filename = DateTime.UtcNow.Ticks + file.FileName;
}
ContentService.Instance.SetContent(file, Helper.CombinePath(uploadPath, filename), true);
return filename;
}

// The above code is called by this code.
HttpPostedFileBase newFile = Request.Files["newFile"] as HttpPostedFileBase;
ContentService service = new ContentService();
blog.Image = service.UploadFile(newFile, string.Format("{0}{1}", Constants.Paths.BlogImages, blog.RowKey));

在将图像文件上传到存储之前,来自 HttpPostedFileBase 的 Property InputStream 看起来没问题(图像的大小符合预期!并且不会引发任何异常)。

真正奇怪的是,这在其他情况下(上传 Power Points 甚至来自 Worker 角色的其他图像)效果很好。调用 SetContent 方法的代码似乎完全相同,并且文件似乎是正确的,因为在正确的位置创建了一个具有零字节的新文件。

请问有人有什么建议吗?我调试了这段代码几十次,但我看不出问题所在。欢迎任何建议!

谢谢

最佳答案

HttpPostedFileBase 的 InputStream 的 Position 属性与 Length 属性具有相同的值(可能是因为我在这个文件之前还有另一个文件 - 我认为很愚蠢!)。

我所要做的就是将 Position 属性设置回 0(零)!

我希望这对将来的人有帮助。

关于file-upload - Azure存储: Uploaded files with size zero bytes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2905754/

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