Use*_*686 4 c# asp.net-web-api postman asp.net-core
我正在使用 .Net Core 开发 Web API,我需要允许客户端上传文件列表(主要是图像)并将它们保存到服务器。
我使用 ASP.NET Core 3.0
这是我的启动文件 Startup.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
namespace ImageUplaodDemo
{
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)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 ImageController 文件 ImageController.cs:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ImageUploadDemo.Controllers
{
[Route("api/[controller]")]
public class ImageController : ControllerBase
{
public static IHostingEnvironment _environment;
public ImageController(IHostingEnvironment environment)
{
_environment = environment;
}
public class FIleUploadAPI
{
public IFormFile files { get; set; }
}
[HttpPost]
public async Task<string> Post(FIleUploadAPI files)
{
if (files.files.Length > 0)
{
try
{
if (!Directory.Exists(_environment.WebRootPath + "\\uploads\\"))
{
Directory.CreateDirectory(_environment.WebRootPath + "\\uploads\\");
}
using (FileStream filestream = System.IO.File.Create(_environment.WebRootPath + "\\uploads\\" + files.files.FileName))
{
files.files.CopyTo(filestream);
filestream.Flush();
return "\\uploads\\" + files.files.FileName;
}
}
catch (Exception ex)
{
return ex.ToString();
}
}
else
{
return "Unsuccessful";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过 POST 操作使用 POSTMAN 上传图像。我将 REQUEST 分配给 POST 并使用 Body 标签下的表单数据,并将 KEY 分配为文件以从桌面上传文件。但我收到以下错误。
我已经尝试了所有可能的方法,但还是遇到了同样的错误...
检查一次代码,需要对代码进行任何修改或对邮递员进行任何修改....
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"detail": null,
"instance": null,
"extensions": {
"traceId": "|2c45b532-43521b159e7b734f."
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
[HttpPost("UploadFile")]
public async Task<string> UploadFile([FromForm] IFormFile file)
{
string fName = file.FileName;
string path = Path.Combine(hostingEnvironment.ContentRootPath, "Images/" + file.FileName);
using (var stream = new FileStream(path, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return file.FileName;
}
Run Code Online (Sandbox Code Playgroud)
您可以在控制器类中使用它。不需要创建 FileUploadAPi 类。
嘿,你尝试过官方文档吗?
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.2
我会尝试使用官方资源来学习这样的主题,而不是随机的博客。
你的控制器调试了吗?您的视图模型中的 IFormFile 是否已映射?
应自动找到控制器并响应:
http(s)://localhost:DEVPORT/api/Image
您可以添加一个返回字符串“Hello World”的索引端点,当您尝试连接浏览器时,您应该会看到一个 Json Response 。
不确定你为什么要这样做:
app.Run(async (context) =>
{
await context.Response.WriteAsync("Controller didn't find anything!");
});
Run Code Online (Sandbox Code Playgroud)
您在帖子请求中发送了正确的正文吗?
| 归档时间: |
|
| 查看次数: |
17842 次 |
| 最近记录: |