【问题标题】:UploadFromFileAsync(string path) throw exception could not find your locationUploadFromFileAsync(字符串路径)抛出异常找不到您的位置
【发布时间】:2019-05-03 10:55:50
【问题描述】:

我正在使用 UploadFromFileAsync(@"E:\test.html");当isend路径时,抛出异常colud not find location,文件存在于我的路径中 谁能帮帮我

fileName = @"E:\\test.html"; 
cloudFile = fileDirectory.GetFileReference(fileName); // Upload a file to the share. 
await cloudFile.UploadFromFileAsync(fileName);
cloudFile.Metadata.Add("FileName", cloudFile.Name); 
cloudFile.Metadata.Add("Status", "1");
await cloudFile.SetMetadataAsync(); 

【问题讨论】:

  • 如果找不到文件,就是找不到文件。首先提供一个代码 sn-p,以及执行代码的位置以及文件的位置。您是否在文件位于本地 PC 上时执行代码?
  • 欢迎来到 Stackoverflow。请阅读FAQHow to Ask。然后回来编辑你的问题。
  • 也许您的 test.html 文件有另一个您在 Windows 资源管理器中看不到的扩展名?
  • 文件名 = @"E:\\test.html"; cloudFile = fileDirectory.GetFileReference(fileName); // 将文件上传到共享。等待 cloudFile.UploadFromFileAsync(fileName); cloudFile.Metadata.Add("文件名", cloudFile.Name); cloudFile.Metadata.Add("状态", "1");等待 cloudFile.SetMetadataAsync();这是代码
  • 既然你已经用 Azure 标记了这个,我猜这个代码是在 Azure 中运行的,它是一个 Web 应用程序。如果是这种情况,那么您的问题是 Azure 应用服务将无法看到您的 E: 驱动器。就像我在我的机器上运行这段代码并期望得到你的文件一样。一种常见的误解是,因为您在自己的机器上浏览网站,所以认为代码就像在您的机器上一样。

标签: c# azure .net-core


【解决方案1】:

如果它是像 webjobs 这样在 azure 中运行的控制台项目,我认为您不能将文件从本地系统上传到 azure 存储。因为它在 azure 中运行并且不知道本地路径。

如果是.net core web项目,比如mvc项目,可以使用IFormFile,允许用户从本地选择文件。

示例代码如下:

在controller.cs(这里,我创建了一个ImagesController.cs):

    public class ImagesController : Controller
    {

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Index(IFormFile file)
        {

            if (file == null || file.Length == 0) return Content("file not selected");
            var filename = Path.GetFileName(file.FileName);
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials("xx", "xxxx"), true);
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference("container_name");
            await blobContainer.CreateIfNotExistsAsync();
            var blockblob = blobContainer.GetBlockBlobReference(filename);

            using (var stream = file.OpenReadStream())
            {
                await blockblob.UploadFromStreamAsync(stream);
            }

            return View();
        }
}

然后在视图中,比如Index.cshtml:

@{
    ViewData["Title"] = "Index";
}

<html>
<head>
    <title>upload files</title>
</head>
<body>
    <form asp-controller="Images" asp-action="Index" method="post"
          enctype="multipart/form-data">

        <input type="file" name="file" />
        <button type="submit">Upload File</button>
    </form>
</body>
</html>

【讨论】:

  • 您好,如果回答对您有帮助,能否帮忙标记为回答?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多