【问题标题】:Azure UploadFromFileAsync returns bad requestAzure UploadFromFileAsync 返回错误请求
【发布时间】:2016-04-14 13:16:23
【问题描述】:

我正在尝试使用 CloudFileClient 简单地上传文件,但遇到了一个奇怪的响应。我直接从 SDK 附带的 Azure 示例文件存储项目中获取示例代码。

我的代码执行完全相同的步骤:

string connection = CloudConfigurationManager.GetSetting("AzureFileStorage");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection);

// Create a CloudFileClient object for credentialed access to File storage.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

// Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference("documents");
await share.CreateIfNotExistsAsync();

// Get a reference to the root directory of the share.        
CloudFileDirectory root = share.GetRootDirectoryReference();

// Put files in this user's folder
CloudFileDirectory dir = root.GetDirectoryReference(User.Identity.GetUserId());
await dir.CreateIfNotExistsAsync();

// Save the document to Azure File Storage
if (System.IO.File.Exists(model.DocumentPath))
{
    CloudFile file = dir.GetFileReference(model.DocumentPath);
    await file.UploadFromFileAsync(model.DocumentPath, FileMode.Open);
}

最后一行返回 400 错误请求。我在我的 Azure 门户中检查子目录已成功创建,并且我可以通过 azure 门户手动上传文件,但是当我尝试示例代码并且我知道文件路径良好时,异步调用总是给我一个黄色带有 400 的页面。

我没有使用日期时间,我的容器名称不是以大写字母开头,我要上传的文件只有几 K

CloudFile 中的 Storage Uri 是 StorageUri {Primary = 'https://mydomain.file.core.windows.net/documents//C:%5Cdev%5Creadme.txt';次要 = ''}

【问题讨论】:

    标签: .net azure model-view-controller cloud storage


    【解决方案1】:

    我认为问题出在以下代码行:

    CloudFile file = dir.GetFileReference(model.DocumentPath);
    

    只是为了测试一下,请将以下内容更改为:

    CloudFile file = dir.GetFileReference("readme.txt");
    

    基本上发生的事情是您将整个文件路径 (C:\dev\readme.txt) 设置为 Azure 文件的名称,而不仅仅是 readme.txt,并且 Azure 文件的名称中有一个 : 字符没有正确逃脱。

    【讨论】:

    • 我的代码原本是这样的,但是在我的 IIS 目录中查找文件时出错,因为它是默认路径。有没有办法可以从文件系统中的任何位置选择文件,因为我希望用户能够从他们的计算机中选择要上传的文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多