Can't upload picture to controller (FormFile, ASP .NET)

0

I am develop gallery application and need to upload files, but can't do this. When i take image and press Sumbit, my program closes and give me exit code -1 (0xffffffff).I take a break point on my controller and i see that it did not call. Later i try again but did not take image, and my controller start work, but of couse IFofmFile is null and it doesn't make sense. I did't see any mistakes and need some help. Thanks.

My View

@model ImageGallery.Models.UploadModel
@using ImageGallery.Controllers
@*@using (Html.BeginForm("UploadNewImageAsync", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))*@

@Html.AntiForgeryToken()
<div class="container body-content ">
    <div class="row upload-container">
        <div class="upload-form drop-shadow">
            <form method="post" asp-controller="Image" asp-action="OnPost"
                  enctype="multipart/form-data"
                  id="upload-form">
                <div class="form-group">
                    <label asp-for="Title"></label>
                    <input asp-for="Title" class="form-control" />
                </div>
                <div class="form-group">
                    <label asp-for="Tags"></label>
                    <input asp-for="Tags" class="form-control" />
                </div>
                <div>
                    <label class="btn btn-file">
                        <input type="file" name="uploadedFile" style="display:normal" />
                    </label>
                </div>
                <div class="simbit">
                    <label class="btn btn-outline-success btn-file">
                        <input type="submit" id="btn-upload" class="btn btn-info" />
                    </label>
                </div>
                <div>
                    <button asp-controller="Image" asp-action="OnPost" type="submit" id="btn-upload" class="btn btn-outline-success">Simbit</button>
                </div>
            </form>
        </div>
    </div>
</div>


And this method

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using ImageGallary.Data;
using ImageGallery.Models;
using ImageGalleryServises;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using ImageGallery.Models;
using Microsoft.Extensions.Logging;

namespace ImageGallery.Controllers
{
    public class ImageController : Controller
    {
        public IImage service;
        private readonly ILogger logger;
        IWebHostEnvironment _appEnvironment;
        public BufferedSingleFileUploadDb FileUpload { set; get; }
        public ImageController(IImage _service, IWebHostEnvironment appEnvironment, ILogger logs)
        {
            logger = logs;
            service = _service;
            _appEnvironment = appEnvironment;
        }
        public IActionResult Upload()
        {
            var model = new UploadModel()
            {

            };
            return View(model);
        }
        [ValidateAntiForgeryToken]
        [HttpPost()]
        public async Task<IActionResult> OnPost([FromForm] string title, [FromForm] string Tags, [FromForm(Name = "uploadedFile")] IFormFile uploadedFile)
        {
            var content = ContentDispositionHeaderValue.Parse(uploadedFile.ContentDisposition);
            if (uploadedFile != null)
            {
                string path = "\\gallery\\" + uploadedFile.FileName;
                var FileName = uploadedFile.FileName.Trim('"');
                using (var fileStream = new System.IO.FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await uploadedFile.CopyToAsync(fileStream);
                }
                service.SetImage(title, Tags, _appEnvironment.WebRootPath+ path);
                service.SaveChanges();
            }
            return RedirectToAction("Index", "Gallery");
        }
    }  
}

Startup:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ImageGallary.Data;
using ImageGalleryServises;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ImageGallery
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var config2 = Configuration.GetConnectionString("ImageGallery");
            services.AddMvc(options => options.EnableEndpointRouting = false);
            services.AddScoped<IImage, Service>();
            services.AddDbContext<ImageGalleryDbContext>(options => options
            .UseSqlServer(Configuration.GetConnectionString("ImageGallery")));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            app.UseStatusCodePages();
            app.UseDeveloperExceptionPage();
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=ImageGallery}/{action=Index}/{id?}");
            });
            logger.LogInformation("Processing request {0}");
            logger.LogDebug($"Handled");
        }
    }
}

Full code on my GitHub Code

asp.net-core
razor
file-upload
controller
iformfile
asked on Stack Overflow Feb 13, 2020 by Andrew Pilikin • edited Feb 13, 2020 by Andrew Pilikin

1 Answer

0

Hah, yandex browser, I think we're all speechless.

answered on Stack Overflow Feb 14, 2020 by Andrew Pilikin

User contributions licensed under CC BY-SA 3.0