我正在开发图库应用程序,需要上传文件,但不能这样做。当我拍摄图像并按下 Sumbit 时,我的程序关闭并给我退出代码 -1 (0xffffffff)。我在控制器上设置了一个断点,我发现它没有调用。后来我再试一次,但没有拍照,我的控制器开始工作,但当然 IFofmFile 为空,它没有意义。我没有看到任何错误,需要一些帮助。谢谢。
我的观点
@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>
而这个方法
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");
}
}
}
启动:
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");
}
}
}
我的 GitHub 代码上的完整 代码