我正在尝试创建一个应用程序,用户可以在其中上传文本文件,并取回更改后的文本。
我使用 React 作为 FE 和 ASP.NET Core 用于 BE 和 Azure 存储作为数据库存储。
这就是我的 HomeController 的样子。我创建了一个单独的“UploadToBlob”方法来发布数据
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration Configuration)
{
_configuration = Configuration;
}
public IActionResult Index()
{
return View();
}
[HttpPost("UploadFiles")]
//OPTION B: Uncomment to set a specified upload file limit
[RequestSizeLimit(40000000)]
public async Task<IActionResult> Post(List<IFormFile> files)
{
var uploadSuccess = false;
string uploadedUri = null;
foreach (var formFile in files)
{
if (formFile.Length <= 0)
{
continue;
}
// read directly from stream for blob upload
using (var stream = formFile.OpenReadStream())
{
// Open the file and upload its data
(uploadSuccess, uploadedUri) = await UploadToBlob(formFile.FileName, null, stream);
}
}
if (uploadSuccess)
{
//return the data to the view, which is react display text component.
return View("DisplayText");
}
else
{
//create an error component to show there was some error while uploading
return View("UploadError");
}
}
private async Task<(bool uploadSuccess, string uploadedUri)> UploadToBlob(string fileName, object p, Stream stream)
{
if (stream is null)
{
try
{
string connectionString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
//Create a unique name for the container
string containerName = "textdata" + Guid.NewGuid().ToString();
// Create the container and return a container client object
BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);
string localPath = "./data/";
string textFileName = "textdata" + Guid.NewGuid().ToString() + ".txt";
string localFilePath = Path.Combine(localPath, textFileName);
// Get a reference to a blob
BlobClient blobClient = containerClient.GetBlobClient(textFileName);
Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);
FileStream uploadFileStream = File.OpenRead(localFilePath);
await blobClient.UploadAsync(uploadFileStream, true);
uploadFileStream.Close();
}
catch (StorageException)
{
return (false, null);
}
finally
{
// Clean up resources, e.g. blob container
//if (blobClient != null)
//{
// await blobClient.DeleteIfExistsAsync();
//}
}
}
else
{
return (false, null);
}
}
}
但是控制台抛出错误,说“'ControllerBase.File(byte[], string)'是一种方法,在给定的上下文中无效(CS0119)”
由于这个错误,另一个错误出现在“'HomeController.UploadToBlob(string, object, Stream)': not all code paths return a value (CS0161)”
我的问题是
- 像我一样创建一个单独的方法是一个更好的主意吗?
- 如何解决有关“文件”在 UploadToBlob 方法中有效的问题?
- 如果我想添加文件类型验证,应该在哪里进行?例如 只有文本文件是 alid
- 如果我想从上传的文本文件中读取文本字符串,我应该在哪里调用
string contents = blob.DownloadTextAsync().Result;
return contents;
- 如何将“内容”传递给我的react组件?像这样的东西?
useEffect(() => {
fetch('Home')
.then(response => response.json())
.then(data => {
setForcasts(data)
})
}, [])
感谢您使用 ASP.NET Core 帮助这个超级新手!