32

画像ファイルを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からのプロパティInputStreamは正常であるように見えます(画像のサイズは予想されるサイズに対応しています!例外はスローされません)。

そして、本当に奇妙なことは、これが他の場合(パワーポイントまたはワーカーロールからの他の画像をアップロードする)で完全に機能することです。SetContentメソッドを呼び出すコードはまったく同じであり、ファイルは正しいようです。これは、ゼロバイトの新しいファイルが正しい場所に作成されるためです。

誰か提案がありますか?このコードを何十回もデバッグしましたが、問題がわかりません。どんな提案でも大歓迎です!

ありがとう

4

2 に答える 2

63

HttpPostedFileBaseのInputStreamのPositionプロパティは、Lengthプロパティと同じ値でした(おそらく、このファイルの前に別のファイルがあったためです-ばかげていると思います!)。

私がしなければならなかったのは、Positionプロパティを0(ゼロ)に戻すことだけでした!

これが将来誰かに役立つことを願っています。

于 2010-05-26T10:40:44.710 に答える
32

これを持ち出し、あなた自身の質問を解決してくれたFabioに感謝します。私はあなたが言ったことにコードを追加したいだけです。あなたの提案は私にとって完璧に機能しました。

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

これで、次を使用してmemoryStreamをアップロードする準備が整いました。

blockBlob.UploadFromStream(memoryStream);
于 2016-08-05T07:26:54.080 に答える