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);
    }

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 Updated the answer
sorry.i did not inderstand ,you mean to path my filePath string into this method ?can you explain please
i fixed it with this line of code :photos.PhotoPath = Path.GetFileName(photos.FormFile.FileName); thanks for help
@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.
|
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);
}

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.