通过 .NET Core Razor 页面将图像上传到 Firebase
Upload image to Firebase through .NET Core Razor pages
我正在尝试使用 .NET Core Razor 页面将图像上传到 Google Firebase。实际上传没有问题。但是对于某些共鸣,文件上传后为 0 kb。我也没有遇到异常或任何其他错误。
upload.cshtml
<form method="post" enctype="multipart/form-data">
<input name="photo" class="form-control" type="file" accept="image/*" />
<input type="submit" value="Add product" />
</form>
upload.cshtml.cs
public async Task OnPostAsync(IFormFile photo)
{
var path = Path.Combine(_env.ContentRootPath, "Uploads", photo.FileName);
var stream = new FileStream(path, FileMode.Create);
await photo.CopyToAsync(stream);
FileName = photo.FileName;
var task = new FirebaseStorage("[project_name].appspot.com")
.Child(FileName)
.PutAsync(stream);
}
这就是我最终成功上传到 Google Firebase 的方式。
var file = Path.Combine(_environment.ContentRootPath, "uploads", Product.Upload.FileName);
using (var fileStream = new FileStream(file, FileMode.Create))
{
await Product.Upload.CopyToAsync(fileStream);
}
// FirebaseStorage.Put method accepts any type of stream.
var stream = System.IO.File.Open(file, FileMode.Open);
//Get file extension
string ext = Path.GetExtension(Product.Upload.FileName);
// you can use CancellationTokenSource to cancel the upload midway
var cancellation = new CancellationTokenSource();
//Upload image to Google Firebase Storage
var task = await new FirebaseStorage([project_name].appspot.com,
new FirebaseStorageOptions
{
ThrowOnCancel = true
})
.Child("[folder_name]")
.Child($"IMAGE_{Guid.NewGuid()}{ext}")
.PutAsync(stream, cancellation.Token);
//Closing stream inorder to delete image from server
stream.Close();
我正在尝试使用 .NET Core Razor 页面将图像上传到 Google Firebase。实际上传没有问题。但是对于某些共鸣,文件上传后为 0 kb。我也没有遇到异常或任何其他错误。
upload.cshtml
<form method="post" enctype="multipart/form-data">
<input name="photo" class="form-control" type="file" accept="image/*" />
<input type="submit" value="Add product" />
</form>
upload.cshtml.cs
public async Task OnPostAsync(IFormFile photo)
{
var path = Path.Combine(_env.ContentRootPath, "Uploads", photo.FileName);
var stream = new FileStream(path, FileMode.Create);
await photo.CopyToAsync(stream);
FileName = photo.FileName;
var task = new FirebaseStorage("[project_name].appspot.com")
.Child(FileName)
.PutAsync(stream);
}
这就是我最终成功上传到 Google Firebase 的方式。
var file = Path.Combine(_environment.ContentRootPath, "uploads", Product.Upload.FileName);
using (var fileStream = new FileStream(file, FileMode.Create))
{
await Product.Upload.CopyToAsync(fileStream);
}
// FirebaseStorage.Put method accepts any type of stream.
var stream = System.IO.File.Open(file, FileMode.Open);
//Get file extension
string ext = Path.GetExtension(Product.Upload.FileName);
// you can use CancellationTokenSource to cancel the upload midway
var cancellation = new CancellationTokenSource();
//Upload image to Google Firebase Storage
var task = await new FirebaseStorage([project_name].appspot.com,
new FirebaseStorageOptions
{
ThrowOnCancel = true
})
.Child("[folder_name]")
.Child($"IMAGE_{Guid.NewGuid()}{ext}")
.PutAsync(stream, cancellation.Token);
//Closing stream inorder to delete image from server
stream.Close();