English 中文(简体)
储存: 2. 载量零的文档
原标题:Azure storage: Uploaded files with size zero bytes

当我把图像文件上载到一个博览时,图像显然能够成功上载(没有错误)。 当我去云层储存室时,该档案存放在那里,但尺寸为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的财产投入Stream似乎被罚款(图像大小与预期的相当!) 没有任何例外。

确实奇怪的是,在其他情况下(从工人的角色中载到电站或甚至其他图像),这项工作是完美的。 所谓“固定”方法的代码似乎完全相同,档案似乎正确,因为在正确的地点建立了无tes的新档案。

是否有任何建议? 我 dozens住了这部法典的数十次,我看不到这个问题。 欢迎任何建议。

增 编

最佳回答

HttpPostedFileBase的InputStream的地位财产与Length财产具有相同价值(可能是因为我此前有过另一个档案——我想是!)。

我必须做的是,将职位财产归还至0(零)!

我希望,这有助于今后有些人。

问题回答

感谢法比奥提出这一问题并解决你自己的问题。 我只想补充你们所说的话。 你的建议对我来说是完美的。

        var memoryStream = new MemoryStream();

        // "upload" is the object returned by fine uploader
        upload.InputStream.CopyTo(memoryStream);
        memoryStream.ToArray();

// After copying the contents to stream, initialize it s position
// back to zeroth location

        memoryStream.Seek(0, SeekOrigin.Begin);

现在,你准备使用:

blockBlob.UploadFromStream(memoryStream);




相关问题
Using a file form field with a Java servlet

I am tring to retrieve a filename or the file itself for use in a java servlet (from a web form). I have a file form field: <form enctype="multipart/form-data" method="post" action="...

Streaming uploaded files directly into a database table

I have several applications that allow users to upload attachments that are stored inside a database table. This has worked fine for several years because it was initially intended for smallish image ...

Asynchronus File Upload with StateServer Mode

I want to implement Asynchronus File Upload. I ve tried ajax:AsyncFileUpload control. It s working fine only with InProc Mode. But I m using StateServer Mode. Any help from anybody? Thanks in ...

How do CMS upload images?

I am just playing around and trying to make a very simple CMS. Right now I what I do right now is I use "FtpWebRequest" to get the file that they want to change around and stick it into a jquery ...

File does not upload from web form in Django

Howdy - I ve written a very simple app to accept job applications including a resume upload. Running the bundled server for development locally, I can successfully upload files via the web form on ...

热门标签