ファイルを.netコアコントローラメソッドにアップロードしようとしましたが、コントローラが起動されたときに 'file'パラメータがnullです。これはサーバ側のコードです...ファイルをAJAXでdotnetコアにアップロードする
[HttpPost]
public async Task<IActionResult> UploadTimetable(long id, IFormFile file)
{
try
{
string fileContent;
using (var reader = new StreamReader(file.ThrowIfNull(nameof(file)).OpenReadStream()))
{
fileContent = await reader.ReadToEndAsync();
}
await routeService.UpdateFromTimetableAsync(id, CsvGenerator.FromString(fileContent));
}
catch (Exception ex)
{
return StatusCode(500, $"Unable to process Timetable ({ex.Message})");
}
return Ok(new ApiServiceJsonResponse<Route>(HttpContextAccessor.HttpContext.Request, id, "routes"));
}
ルートは正しくトリガされますが、 'file'の値はnullです。
私はAJAXリクエストボディに何も見ることができないので、問題はクライアントサイドに関係すると思われます。これは、このように構築されています...
/**
* An AJAX request wrapper.
* Usage of this enables testing AJAX calls.
*
* @export AjaxRequest
* @class AjaxRequest
* @extends {AjaxRequest}
*/
export default class AjaxRequest {
/**
* Creates an instance of AjaxRequest.
* @param {any} { url, type, contentType, cache, processData, data, successCallback, errorCallback }
*
* @memberOf AjaxRequest
*/
constructor({ url, type, contentType, cache, processData, data, successCallback, errorCallback }) {
Guard.throwIf(url, "url");
let emptyFunc =() => {};
this.url = url;
this.type = type.toUpperCase() || "GET";
this.contentType = contentType !== undefined ? contentType : "application/json; charset=utf-8";
this.processData = processData !== undefined ? processData : true;
this.dataType = "json";
this.cache = cache || false;
this.data = data ? JSON.stringify(data) : undefined;
this.successCallback = successCallback || emptyFunc;
this.errorCallback = errorCallback || emptyFunc;
}
/**
* Executes the AJAX request.
*
* @memberOf AjaxRequest
*/
execute() {
$.ajax({
url: this.url,
type: this.type,
contentType: this.contentType,
processDAta: this.processData,
dataType: this.dataType,
cache: this.cache,
data: this.data,
success: this.successCallback,
error: this.errorCallback
});
}
/**
* Gets a File Upload request.
*
* @static
* @param {string} url
* @param {array} files The files to upload
* @param {function} successCallback
* @param {function} errorCallback
* @returns
*
* @memberOf AjaxRequest
*/
static fileUpload(url, files, successCallback, errorCallback) {
let data = new FormData();
for (let i = 0; i < files.length; i++) {
let file = files[i];
data.append('file', file, file.name);
}
return new AjaxRequest({
url: url,
type: 'POST',
data: data,
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
successCallback: successCallback,
errorCallback: errorCallback
});
}
}
「ファイルアップロード」機能は、ターゲットURLとモーダルでファイル入力HTMLコントロールからのファイルリストで呼び出されます。この時点のconsole.logは、問題がこれらのポイントの間のどこかにあるように、ファイルリストが予想通りに渡されることを示します。
クロムでは、リクエストにフォームデータ要素が表示されません。私は本当に見たいと思っています。データオブジェクトの作成に何か問題があると思いますが、わかりません。 Chromeから
...私は私がいないフォームデータを示すであろうことを期待する上GENERAL
Request URL:https://localhost:44333/Route/UploadTimetable/60018
Request Method:POST
Status Code:500
Remote Address:[::1]:44333
RESPONSE HEADERS
content-type:text/plain; charset=utf-8
date:Wed, 08 Mar 2017 18:02:41 GMT
server:Kestrel
status:500
x-powered-by:ASP.NET
x-sourcefiles:=?UTF-8?B?QzpcRGV2ZWxvcG1lbnRcQ2xpZW50c1xFc290ZXJpeFxNT0RMRSBPcGVyYXRpb25zXHNyY1xFc290ZXJpeC5Nb2RsZS5Qb3J0YWx3ZWJcUm91dGVcVXBsb2FkVGltZXRhYmxlXDYwMDE4?=
REQUEST HEADERS
:authority:localhost:44333
:method:POST
:path:/Route/UploadTimetable/60018
:scheme:https
accept:application/json, text/javascript, */*; q=0.01
accept-encoding:gzip, deflate, br
accept-language:en-GB,en-US;q=0.8,en;q=0.6
cache-control:no-cache
content-length:2
content-type:text/plain;charset=UTF-8
cookie: {removed}
origin:https://localhost:44333
pragma:no-cache
referer:https://localhost:44333/Route/60018?Message=The%20route%20details%20have%20been%20updated.
user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
x-requested-with:XMLHttpRequest
REQUEST PAYLOAD
{}
ザ・?