Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Azure block blob存储连续上载同一文件失败_Azure_Azure Storage_Azure Storage Blobs - Fatal编程技术网

Azure block blob存储连续上载同一文件失败

Azure block blob存储连续上载同一文件失败,azure,azure-storage,azure-storage-blobs,Azure,Azure Storage,Azure Storage Blobs,我正在使用Azure Block Blob存储来保存我的文件。这是我上传文件的代码 对于同一请求中的同一个文件,我将调用以下方法两次; 方法的第一次调用按预期保存文件,但第二次调用将文件保存为0长度,因此我无法显示图像,并且不会发生错误 [HttpPost] public ActionResult Index(HttpPostedFileBase file) { UploadFile(file); UploadFile(file);

我正在使用Azure Block Blob存储来保存我的文件。这是我上传文件的代码

对于同一请求中的同一个文件,我将调用以下方法两次; 方法的第一次调用按预期保存文件,但第二次调用将文件保存为0长度,因此我无法显示图像,并且不会发生错误

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        UploadFile(file);
        UploadFile(file); 
        return View();
    }

      public static string UploadFile(HttpPostedFileBase file){

        var credentials = new StorageCredentials("accountName", "key");

        var storageAccount = new CloudStorageAccount(credentials, true);

        var blobClient = storageAccount.CreateCloudBlobClient();

        var container = blobClient.GetContainerReference("images");

        container.CreateIfNotExists();

        var containerPermissions = new BlobContainerPermissions
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        };

        container.SetPermissions(containerPermissions);

        var blockBlob = container.GetBlockBlobReference(Guid.NewGuid().ToString());

        blockBlob.Properties.ContentType = file.ContentType;

        var azureFileUrl = string.Format("{0}", blockBlob.Uri.AbsoluteUri);

        try
        {
            blockBlob.UploadFromStream(file.InputStream);
        }

        catch (StorageException ex)
        {
            throw;
        }
        return azureFileUrl ;
    }
我只是找到下面的解决办法,这是奇怪的像我的,但它没有帮助

有什么想法吗?
谢谢

您需要将流的位置重置回开始位置。将这一行放在UploadFile方法的顶部

file.InputStream.Seek(0, SeekOrigin.Begin);

顺便说一句,你为什么要两次调用这个方法?谢谢你的回复,这解决了我的问题。实际上,我要重构这个方法来保存不同大小的图像,我需要在同一个post操作中保存同一文件的2个版本,所以无论如何,我将使用不同的宽度和高度参数调用它两次。seek方法的逻辑是什么?我正在调用该方法,因此我实例化了块blob等的不同实例。为什么我需要再次从0开始?是的,但源文件(发布的文件)的文件指针在流式传输到第一个blob后位于文件的末尾。因此,如果你想再次阅读,你必须把它移回到开头。