0

I want to save file in my localdb how to convert fileName to absolute uri & pass it to my setImage Method

[HttpPost]
    public async Task <IActionResult> UploadNewImage(IFormFile file, string title, string tags)
    {
        var content = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
        var fileName = content.FileName.Trim('"');

        await _imageService.SetImage(title, tags, uri);
        return RedirectToAction("Index", "Gallery");
    }

Here is my SetImage method

public async Task SetImage(string title, string tags, Uri uri)
    {
        var image = new GalleryImage
        {
            Title = title,
            Tags = ParseTags(tags),
            Url = uri.AbsolutePath,
            Created = DateTime.Now
        };
        _ctx.Add(image);
        await _ctx.SaveChangesAsync();
    }
saeef ahmed
  • 711
  • 3
  • 9
  • 26
  • 2
    I suggest not storing the absolute uri in table. Just store the unique file name so that the absolute uri can be built later when you want to. What if tomorrow your business decide to change the site base url from `thefacebook` to `facebook` ? – Shyju Dec 10 '17 at 02:25
  • Also your code is not storing the image anywhere! Refer this [How to upload files in asp.net core?](https://stackoverflow.com/questions/35379309/how-to-upload-files-in-asp-net-core) – Shyju Dec 10 '17 at 02:36

1 Answers1

0

Try this:

public async Task SetImage(string title, string tags, Uri uri)
{
    var image = new GalleryImage
    {
        Title = title,
        Tags = ParseTags(tags),
        Url = uri.AbsoluteUri,
        Created = DateTime.Now
    };
    _ctx.Add(image);
    await _ctx.SaveChangesAsync();
}
trashr0x
  • 6,457
  • 2
  • 29
  • 39
Mohammed
  • 396
  • 4
  • 15