0

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. there is error i get because of this:

"Not allowed to load local resource :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);
    }
rustam13
  • 49
  • 1
  • 7

2 Answers2

3

Break the code down:

var localPath = Path.Combine("Upload", "file.jpg");

var fullPath = Path.Combine(_appEnvironment.ContentRootPath, localPath);

Save the localPath to PhotoPath

Edit

Okey so now bring up your PhotoPath in a View, and make it target a file stream action.

[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.

You can't target dynamically added files by using the following: ~/path/image.jpg for your source.

Anton Toshik
  • 2,621
  • 2
  • 19
  • 42
  • hi ,this did not help ,still getting this ,still getting full path :Not allowed to load local resource: file:///C:/Users/rustam/Desktop/FightSports-backend/FightSports/Uploads/unnamed.jpg – rustam13 Oct 02 '18 at 18:32
  • @rustam13 Updated the answer – Anton Toshik Oct 02 '18 at 20:19
  • sorry.i did not inderstand ,you mean to path my filePath string into this method ?can you explain please – rustam13 Oct 02 '18 at 20:53
  • i fixed it with this line of code :photos.PhotoPath = Path.GetFileName(photos.FormFile.FileName); thanks for help – rustam13 Oct 02 '18 at 22:30
  • @rustam13 don't use `FileName` property of the File to save your file, it will cause issues when uploading in IE, and don't forget to mark the question answered if you think this answers your question. – Anton Toshik Oct 03 '18 at 06:45
  • thank you, but i cant mark question, i have not enough reputaion – rustam13 Oct 03 '18 at 17:47
0

Make sure you have configured IFileProvider pointing to your Uploads folder in Configure method of Startup class:

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);
}
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • hi,i did that ,still geting this , still getting full path : Not allowed to load local resource: file:///C:/Users/rustam/Desktop/FightSports-backend/FightSports/Uploads/unnamed.jpg – rustam13 Oct 02 '18 at 18:31
  • Check this https://stackoverflow.com/questions/39007243/cannot-open-local-file-chrome-not-allowed-to-load-local-resource – Dmitry Pavlov Oct 03 '18 at 11:09