Azure存储:上传的大小为零的文件 [英] Azure storage: Uploaded files with size zero bytes

查看:233
本文介绍了Azure存储:上传的大小为零的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我上传一个图像文件到一个斑点,图像上传显然成功(没有错误)。当我到云存储工作室,文件是在那里,但大小为0(零)字节。



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

  //这两个方法属于ContentService类,用于在存储中上传
//文件。
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)
{
返回null;
}

字符串文件名;
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);
返回文件名;
}

//上面的代码被这段代码调用。
HttpPostedFileBase newFile = Request.Files [newFile]作为HttpPostedFileBase;
ContentService service = new ContentService();
blog.Image = service.UploadFile(newFile,string.Format({0} {1},Constants.Paths.BlogImages,blog.RowKey));

在图像文件上传到存储之前,HttpPostedFileBase中的Property InputStream看起来很好图像的大小对应于预期的!并没有抛出任何异常)。

真正奇怪的是,在其他情况下,工人角色的点或甚至其他图像)。调用SetContent方法的代码看起来是完全一样的,文件似乎是正确的,因为在正确的位置创建了一个零字节的新文件。



有什么建议吗?我调试了这个代码几十次,我看不到问题。欢迎任何建议!



谢谢

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

所有我必须是的,将位置属性设置为0(零)!

我希望这将有助于未来的人。


When I upload an image file to a blob, the image is uploaded apparently successfully (no errors). When I go to cloud storage studio, the file is there, but with a size of 0 (zero) bytes.

The following is the code that I am using:

// 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));

Before the image file is uploaded to the storage, the Property InputStream from the HttpPostedFileBase appears to be fine (the size of the of image corresponds to what is expected! And no exceptions are thrown).

And the really strange thing is that this works perfectly in other cases (uploading Power Points or even other images from the Worker role). The code that calls the SetContent method seems to be exactly the same and file seems to be correct since a new file with zero bytes is created at the correct location.

Does any one have any suggestion please? I debugged this code dozens of times and I cannot see the problem. Any suggestions are welcome!

Thanks

解决方案

The Position property of the InputStream of the HttpPostedFileBase had the same value as the Length property (probably because I had another file previous to this one - stupid I think!).

All I had to do was to set the Position property back to 0 (zero)!

I hope this helps somebody in the future.

这篇关于Azure存储:上传的大小为零的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆