上传图片asp.net Core [英] uploading image asp.net Core
问题描述
我是ASP.net COre的新手,我想上传一个文件(一个图像)并将其存储在一个安全的文件夹中.我一直在关注此教程(在ASP.NET Core中上传文件),但是我不知道如何将其存储在服务器上刚刚上传的文件中 这是html:
i am new to ASP.net COre and i would like to upload a file ( an image) and store it in a secific Folder. ihave been following this tuto (File uploads in ASP.NET Core) but i dont know how to store it in a file it is just uploaded on the server this is the html :
<form method="post" enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Index">
<div class="form-group">
<div class="col-md-10">
<p>Upload one or more files using this form:</p>
<input type="file" name="files" multiple />
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Upload" />
</div>
</div>
这是控制器
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
// full path to file in temp location
var filePath = Path.GetTempFileName();
foreach (var formFile in files)
{
if (formFile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
// process uploaded files
// Don't rely on or trust the FileName property without validation.
return Ok(new { count = files.Count, size, filePath});
}
这是我正在使用的课程
public interface IFormFile
{
string ContentType { get; }
string ContentDisposition { get; }
IHeaderDictionary Headers { get; }
long Length { get; }
string Name { get; }
string FileName { get; }
Stream OpenReadStream();
void CopyTo(Stream target);
Task CopyToAsync(Stream target, CancellationToken cancellationToken = null);
}
我只需要将图像存储在如:C:\ Users \ DESKTOP-1603 \ Desktop \ GestionSyndic \ GestionSyndic \ src \ WebApplication1 \ wwwroot \ images \的文件夹中 并更改图像的名称谢谢:D
i just need to store the image in a folder like :C:\Users\DESKTOP-1603\Desktop\GestionSyndic\GestionSyndic\src\WebApplication1\wwwroot\images\ and change the name of the image thank you :D
推荐答案
在ASP.NET Core MVC中通过简单的步骤上传文件
File upload in ASP.NET Core MVC in simple steps
1.Demo.cshtml查看代码段
1.Demo.cshtml View Code snippet
<form method="post" enctype="multipart/form-data" asp-controller="Demo" asp-action="Index">
<div class="form-group">
<div class="col-md-10">
<p>Upload one or more files using this form:</p>
<input type="file" name="files" multiple />
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input type="submit" value="Upload" />
</div>
</div>
- DemoController
为了显示演示,我创建了一个演示控制器,其中有2个操作方法,一个用于处理Get Request,另一个用于处理发布请求.
For showing demo i have created a Demo Controller which has 2 Action Methods in it one to handle Get Request and another to handle post request.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Net.Http.Headers;
namespace WebApplication6.Controllers
{
public class DemoController : Controller
{
private readonly IHostingEnvironment _environment;
// Constructor
public DemoController(IHostingEnvironment IHostingEnvironment)
{
_environment = IHostingEnvironment;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string name)
{
var newFileName = string.Empty;
if (HttpContext.Request.Form.Files != null)
{
var fileName = string.Empty;
string PathDB = string.Empty;
var files = HttpContext.Request.Form.Files;
foreach (var file in files)
{
if (file.Length > 0)
{
//Getting FileName
fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
//Assigning Unique Filename (Guid)
var myUniqueFileName = Convert.ToString(Guid.NewGuid());
//Getting file Extension
var FileExtension = Path.GetExtension(fileName);
// concating FileName + FileExtension
newFileName = myUniqueFileName + FileExtension;
// Combines two strings into a path.
fileName = Path.Combine(_environment.WebRootPath, "demoImages") + $@"\{newFileName}";
// if you want to store path of folder in database
PathDB = "demoImages/" + newFileName;
using (FileStream fs = System.IO.File.Create(fileName))
{
file.CopyTo(fs);
fs.Flush();
}
}
}
}
return View();
}
}
}
调试时快照
Snapshot while Debugging
存储图像的文件夹的位置
Location of folder where Images are stored
这篇关于上传图片asp.net Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!