Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to upload photo and add it in the database and save a file in the web root directory Asp.Net Mvc 6

$
0
0
// PublisherInfosRepository
public class PublisherInfosRepository
    {
        private readonly ApplicationDbContext context = new ApplicationDbContext();
        private readonly IApplicationEnvironment _appEnv;

        public int UploadImageInDataBase(IFormFile file, PublisherInfos publisherInfos)
        {
            publisherInfos.CoverImage = ConvertToBytes(file);
            var pubInfos = new PublisherInfos
            {
                ImageSize = publisherInfos.ImageSize,
                FileName = publisherInfos.FileName,
                CoverImage = publisherInfos.CoverImage
            };
            context.PublisherInfos.Add(pubInfos);
            int i = context.SaveChanges();
            if (i == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        private byte[] ConvertToBytes(IFormFile image)
        {
            byte[] imageBytes = null;
            var fileName = ContentDispositionHeaderValue
                    .Parse(image.ContentDisposition)
                    .FileName
                    .Trim('"'); // FileName returns "fileName.ext" (with double quotes) in beta 3
            var fileParth = _appEnv.ApplicationBasePath + "\\wwwroot\\images\\publisherinfos\\";
            var directory = new DirectoryInfo(_appEnv.ApplicationBasePath + "\\wwwroot\\images\\publisherinfos\\");
            image.SaveAsAsync(fileParth);
            Stream stream = image.OpenReadStream();
            BinaryReader reader = new BinaryReader(stream);
            imageBytes = reader.ReadBytes((int)image.Length);
            return imageBytes;
        }
    }
// PublisherInfosController

public IActionResult Create(PublisherInfos publisherInfos)
{
            if (ModelState.IsValid)
            {
                IFormFile file = Request.Form.Files["ImageData"];
                var service = new PublisherInfosRepository();
                int i = service.UploadImageInDataBase(file, publisherInfos);
                if (i == 1)
                {
                    // Add file size and file name into Database
                    _context.PublisherInfos.Add(publisherInfos);
                    _context.SaveChanges();
                    return RedirectToAction("Index", new { Message = PublisherInfoMessageId.DataloadSuccess });
                }
            }
}
// Create PublisherInfos Form

<form asp-action="Create"><div class="form-horizontal"><h4>PublisherInfos</h4><hr /><div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div><div class="form-group"><label asp-for="PubId" class="col-md-2 control-label"></label><div class="col-md-10"><select asp-for="PubId" asp-items="@ViewBag.Publishers" class="form-control"></select></div></div><div class="form-group"><label asp-for="CoverImage" class="col-md-2 control-label"></label><div class="col-md-10"><form asp-action="CoverImage" asp-controller="PublisherInfos" method="post" enctype="multipart/form-data"><div class="btn btn-default browseimg"><p>Browse...</p><input type="file" name="ImageData" id="ImageData" onchange="fileCheck(this);" /></div></form></div></div><div class="form-group"><label asp-for="ImageSize" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="ImageSize" class="form-control" /><span asp-validation-for="ImageSize" class="text-danger" /></div></div><div class="form-group"><label asp-for="FileName" class="col-md-2 control-label"></label><div class="col-md-10"><input asp-for="FileName" class="form-control" /><span asp-validation-for="FileName" class="text-danger" /></div></div><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-primary" /></div></div></div></form>
I have the following problem:
NullReferenceException: Object reference not set to an instance of an object.

    OLMS.Repositories.PublisherInfosRepository.ConvertToBytes(IFormFile image) in PublisherInfosRepository.cs
                    var fileName = ContentDispositionHeaderValue
    OLMS.Repositories.PublisherInfosRepository.UploadImageInDataBase(IFormFile file, PublisherInfos publisherInfos) in PublisherInfosRepository.cs
                    publisherInfos.CoverImage = ConvertToBytes(file);
    OLMS.Controllers.PublisherInfosController.Create(PublisherInfos publisherInfos) in PublisherInfosController.cs
                        int i = service.UploadImageInDataBase(file, publisherInfos);
    --- End of stack trace from previous location where exception was thrown ---

Can some experts of asp.net 5 Mvc 6 help me to resolve the problem?




Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>