2 日間、メディア ファイルのアップロードの問題に直面しています。コミュニティ フォーラムで提供されている解決策はすべて試しました。しかし、まだ問題に直面しています。これまでに試したコードを共有します。
Asp.netコアコントローラー
//[HttpPost]
//[RequestFormLimits(MultipartBodyLengthLimit = 100000000)]
//[RequestSizeLimit(100000000)]
[HttpPost("UploadVideo"),DisableRequestSizeLimit,RequestFormLimits(MultipartBodyLengthLimit =Int16.MaxValue,ValueLengthLimit =Int32.MaxValue)]
public IActionResult Upload([FromForm] IFormFile media)
{
if (media != null)
{
//new HomeController().Run(media).Wait();
return Ok(media.FileName);
}
またはAPIコントローラー
[HttpPost("UploadVideo"), DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = Int16.MaxValue, ValueLengthLimit = Int32.MaxValue)]
public ActionResult Post([FromForm] IFormFile media)
{
return new JsonResult(media.FileName);
}
Jクエリコード
let ajax = async (_formData) => {
$.ajax({
url: '/Home/UploadVideo',
type: "POST",
//timeout: 0,
processData: false, // Not to process data
//mimeType: "multipart/form-data",
contentType: false, // Not to set any content header
data: _formData,
success: function (result) {
console.log(result);
},
error: function(err) {
let { statusText, status } = err;
console.log({ statusText, status});
}
});
//let headers = new Headers();
//headers.append("Content-Type", "multipart/form-data");
//try {
// let response = await fetch(
// //`/api/FileApi/UploadVideo`,
// '/Home/UploadVideo',
// { method: "POST", body: _formData },
// headers
// );
// return response.json();
//} catch (e) {
// console.log(e)
//}
};
$('body').on('click', '#upload', (e) => {
e.preventDefault();
let formData = new FormData();
let getFile = document.getElementById("media-file");
//$('#media-file').get(0);
if (getFile.files != null) {
let media = getFile.files[0];
formData.append('media', media);
console.log(media);
ajax(formData);
}
});
HTMLコード
<div>
<form>
<input type="file" id="media-file" name="media" />
<button class="btn btn-primary" id="upload">Upload</button>
</form>
</div>
Startup.cs
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.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
//services.AddHttpClient();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//app.UseFileServer();
app.UseStaticFiles();
app.UseCookiePolicy();
//app.UseCors(builder =>
//builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
あなたが私を助けてくれるなら、私はあなたにとても感謝します.