public IActionResult Save(IList < IFormFile > UploadFiles)
{
var status = Response.StatusCode;
try {
foreach(var file in UploadFiles)
{
if (UploadFiles != null) {
var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
filename = hostingEnv.WebRootPath + $@"{filename}";
if (!System.IO.File.Exists(filename)) {
using(FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
else {
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";
}
}
}
}
catch (Exception e)
{
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
return Content("");
}
|
|
public IActionResult Save(IList < IFormFile > UploadFiles) { var status = Response.StatusCode; try { foreach(var file in UploadFiles) { if (UploadFiles != null) { var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); filename = hostingEnv.WebRootPath + $@"{filename}"; if (!System.IO.File.Exists(filename)) { using(FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } } else { Response.Clear(); Response.StatusCode = 204; Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists."; } } } } catch (Exception e) { Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.StatusCode = 204; Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content"; Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message; } return Content(""); } |
public IActionResult Save(IList<IFormFile> UploadFiles)
{
var status = Response.StatusCode;
try
{
foreach (var file in UploadFiles)
{
if (UploadFiles != null)
{
var filename = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
filename = hostingEnv.WebRootPath + $@"\{filename}";
if (!System.IO.File.Exists(filename))
{
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
else
{
Response.Clear();
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File already exists.";
}
}
}
}
catch (Exception e)
{
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.StatusCode = 204;
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "No Content";
Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
}
return Ok(BadRequest(ModelState));
}
|
|
Response
implements Body
, so it also has the following properties available to it:
Body.body
Read onlyReadableStream
of the body contents.Body.bodyUsed
Read onlyBoolean
that declares whether the body has been used in a response yet.
<form id="formElem" method="post">
<ejs-uploader id="UploadFiles" autoUpload="false"></ejs-uploader>
<input type="button" value="Post" onclick="btnClick()" />
</form>
<script>
function btnClick() {
var uploaderObj = document.getElementById('UploadFiles').ej2_instances[0];
var form = $('form')[0];
var formData = new FormData(form);
formData.append("file", uploaderObj.element.files[0]);
$.ajax({
type: "POST",
url: "/Home/Save",
data: formData,
contentType: false,
processData: false,
success: function (data) {
console.log(data);
}
});
}
</script>
|