[英]Asp.net Core File Upload
when i save image, image save successfully in database, but it takes full image path like this C:\\Users....\\Uploads\\employee.jpg i dont want like this, i need to save image path somehting like this ~Uploads\\employee.jpg and in specific folder and same path should save in database, also if someone show me after saving correct path how i can view that image. 当我保存图像时,图像成功保存到数据库中,但是它需要完整的图像路径,例如C:\\ Users .... \\ Uploads \\ employee.jpg我不想要这样,我需要以某种方式保存图像路径〜Uploads \\ employee.jpg以及特定的文件夹和相同的路径应该保存在数据库中,如果有人在保存正确的路径后向我显示我如何查看该图像。 there is error i get because of this: 因为这个我有错误:
"Not allowed to load local resource :file:///C:/......" “不允许加载本地资源:file:/// C:/ ......”
thank you! 谢谢!
my code: 我的代码:
[HttpPost]
public async Task<IActionResult> Create(Photos photos)
{
if (ModelState.IsValid)
{
var filePath =
Path.Combine(_appEnvironment.ContentRootPath,
"Uploads", photos.FormFile.FileName);
photos.PhotoPath = filePath;
using (var stream = new FileStream(filePath, FileMode.Create))
{
await photos.FormFile.CopyToAsync(stream);
}
_context.Add(photos);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["NewsId"] = new SelectList(_context.News, "NewsId",
"NewsBigTitle", photos.NewsId);
return View(photos);
}
Break the code down: 分解代码:
var localPath = Path.Combine("Upload", "file.jpg");
var fullPath = Path.Combine(_appEnvironment.ContentRootPath, localPath);
Save the localPath
to PhotoPath
将localPath
保存到PhotoPath
Edit 编辑
Okey so now bring up your PhotoPath
in a View, and make it target a file stream action. Okey,现在在视图中显示您的PhotoPath
,并将其定位为文件流操作。
[HttpGet("path/{image}")]
public FileStreamResult Image(string image)
{
var result = new FileStreamResult(new FileStream(
Path.Combine(_appEnvironment.ContentRootPath, image),
FileMode.Open,
FileAccess.Read), "image/<your_mime>");
return result;
}
The best way I think is to create a new string in the following format http://example.com/path/image.jpg
and bind it to src
. 我认为最好的方法是使用以下格式http://example.com/path/image.jpg
创建新字符串并将其绑定到src
。
You can't target dynamically added files by using the following: ~/path/image.jpg
for your source. 您不能使用以下内容来定位动态添加的文件: ~/path/image.jpg
作为源。
Make sure you have configured IFileProvider
pointing to your Uploads
folder in Configure
method of Startup
class: 确保已配置IFileProvider
使其指向Startup
类的Configure
方法中的Uploads
文件夹:
app.UseStaticFiles(); // Default one
// Adding one more location - folder named `Uploads` to be a custom static files folder.
// The `Uploads` folder is available in the content root directory of the application (where your `Controllers` folder.
// You can point it of course inside `wwwroot` - use `env.WebRootPath` instead)
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "Uploads")),
RequestPath = new PathString("/Uploads")
});
Once you do this you should be able to upload the file this way in your controller action: 完成此操作后,您应该可以在控制器操作中以这种方式上传文件:
var filePath = Path.Combine(_environment.ContentRootPath, "Uploads", photos.FormFile.FileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await photos.FormFile.CopyToAsync(stream);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.