.Net framework 4.6.1
I have the below code set up to upload a file to the server.
If the file name is not present in the data, I want to throw a specific error message back to the fetch so I can display it to the user.
What is the proper way to return the error message?
fetch(url, {
method: 'POST',
body: data,
}).then(function (response) {
if (!response.ok) {
return response.json()
.then(function (obj) {
throw Error(obj.ErrorMessage)
})
}
else {
return response.json()
.then(json => {
/*further processing */
})
}
}).catch(/* work with the error */)
/// Uploads the file chunk.
/// </summary>
/// <param name="collection">The collection.</param>
/// <returns>
/// ActionResult
/// </returns>
[HttpPost]
[ValidateAntiForgeryToken]
[Log(ClickTrackingItemTypeConstants.DataPost)]
public async Task<ActionResult> UploadFileChunk(FormCollection collection)
{
var fileDataParam = JsonConvert.DeserializeObject<UploadFileChunkFileDataModel>(collection["fileDataParam"]);
if (fileDataParam == null)
{
throw new RubixGenericException(nameof(fileDataParam));
}
fileDataParam.FileName = string.Empty;
if (string.IsNullOrWhiteSpace(fileDataParam.FileName) == true)
{
throw new Exception("error...");
}
fileManagerUtilities.AppendContentToFile(fileDataParam.FileGuid, fileDataParam.FileExtension, Request.Files[0]);
if (fileDataParam.ChunkIndex == (fileDataParam.ChunkTotalCount - 1))
{
System.Diagnostics.Debug.WriteLine("file can be uploaded now");
}
return Json(new { success = true });
}
Here is what the response looks like: