繁体   English   中英

从 Razor 页面上传到 Azure 文件共享

[英]Uploading to Azure File Share from a Razor Page

我试图找到一个从 razor 页面将文件上传到 Azure 文件共享或 blob 存储的示例。 我使用教程中的这段代码,但只将文件复制到本地文件系统。 现在我在 stackoverflow 上找到了这篇文章,但它不起作用。 我得到了错误

 var storageAccount = CloudStorageAccount.Parse(< your creds here>); 
 the name "CloudStorageAccount does not exist in the current context"

我正在使用 Visual Studio 2022、.Net Core 2.2。 任何帮助将非常感激。

最初我什至得到了错误。在此处输入图像描述

  • CloudStorageAccount所需的 NuGet package 是WindowsAzure.Storage

在此处输入图像描述

  • 但是在 Package 管理器中,您可以看到WindowsAzure.Storage已弃用。

  • 您还可以看到提到了备用 package Azure.Storage.Blobs在此处输入图像描述

  • 安装最新的Azure.Storage.Blobs NuGet package。

  • 当我尝试安装Azure.Storage.Blobs package 时,出现以下错误。

 "System.IO.Hashing doesn't support $(TargetFramework). Consider >updating the TargetFramework to.netcoreapp3.1 or later." />

在此处输入图像描述

该错误明确表示要更新并使用.NET Core最新版本。

我可以通过安装以下 NuGet Microsoft.Azure.Storage.DataMovement来解决.Net Core 2.1中的问题

在此处输入图像描述

  • using Microsoft.Azure.Storage添加 using 命名空间

在此处输入图像描述

我的.csproj文件

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.Azure.Storage.DataMovement" Version="2.0.4" />
  </ItemGroup>
</Project>

我的控制器.cs

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.File;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using WebAppMVC.Models;

namespace WebAppMVC.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Upload(IFormFile file)
        {
            if (file != null)
            {
                using (var stream = new MemoryStream())
                {
                    try
                    {
                        
                        await file.CopyToAsync(stream);
                        stream.Seek(0, SeekOrigin.Begin);

                        
                        var filename = file.FileName;
                        var storageAccount = CloudStorageAccount.Parse("YOUR CREDS HERE");
                        var client = storageAccount.CreateCloudFileClient();
                        var shareref = client.GetShareReference("YOUR FILES SHARE");
                        var rootdir = shareref.GetRootDirectoryReference();
                        var fileref = rootdir.GetFileReference(filename);
                        await fileref.DeleteIfExistsAsync();
                        await fileref.UploadFromStreamAsync(stream);

                        return Ok(new { fileuploaded = true });
                    }
                    catch (Exception ex)
                    {
                        return BadRequest(ex);
                    }
                }
            }
            else
            {
                return BadRequest(new { error = "there was no uploaded file" });
            }
        }
      
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM