I'm writing a file upload in ASP.Net Core and I'm trying to update a progress bar but when the Progress action is called from javascript, the session value isn't updated properly.
The progress is saved in the user Session using:
public static void StoreInt(ISession session, string key, int value)
{
session.SetInt32(key, value);
}
The upload:
$.ajax(
{
url: "/Upload",
data: formData,
processData: false,
contentType: false,
type: "POST",
success: function (data) {
clearInterval(intervalId);
$("#progress").hide();
$("#upload-status").show();
}
}
);
Getting the progress value:
intervalId = setInterval(
function () {
$.post(
"/Upload/Progress",
function (progress) {
$(".progress-bar").css("width", progress + "%").attr("aria-valuenow", progress);
$(".progress-bar").html(progress + "%");
}
);
},
1000
);
Upload Action:
[HttpPost]
public async Task<IActionResult> Index(IList<IFormFile> files)
{
SetProgress(HttpContext.Session, 0);
[...]
foreach (IFormFile file in files)
{
[...]
int progress = (int)((float)totalReadBytes / (float)totalBytes * 100.0);
SetProgress(HttpContext.Session, progress);
// GetProgress(HttpContext.Session) returns the correct value
}
return Content("success");
}
Progress Action:
[HttpPost]
public ActionResult Progress()
{
int progress = GetProgress(HttpContext.Session);
// GetProgress doesn't return the correct value: 0 when uploading the first file, a random value (0-100) when uploading any other file
return Content(progress.ToString());
}
Alright, I used the solution suggested by #FarzinKanzi which is processing the progress client side instead of server side using XMLHttpRequest:
$.ajax(
{
url: "/Upload",
data: formData,
processData: false,
contentType: false,
type: "POST",
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var progress = Math.round((evt.loaded / evt.total) * 100);
$(".progress-bar").css("width", progress + "%").attr("aria-valuenow", progress);
$(".progress-bar").html(progress + "%");
}
}, false);
return xhr;
},
success: function (data) {
$("#progress").hide();
$("#upload-status").show();
}
}
);
Thank you for your help.
Related
i have this jquery code, sending items to controller and download from controller, everything is fine
downloads is fine, i check response from in Chrome Network Tab is okay. but success function never run after process done. (i'm using async:false; already)
$(document).on('click', '#baslat', function (e) {
var token = $("#token").val();
var islemler = [];
var secililer = [];
$.each($("input[class='cc']:checked"), function () {
var islem = {};
islem.IslemTuru = $(this).attr("id");
islemler.push(islem);
});
$.each($("tr[class='sec']"), function () {
if ($(this).children('td:eq(1)').children("input[type='checkbox']").prop('checked')) {
var beyan = {};
beyan.Id = $(this).attr("id");
beyan.TahakkukId = $(this).data("id");
beyan.KisaKod = $(this).children('td:eq(2)').html();
beyan.BeyannameTuru = $(this).children('td:eq(4)').html();
beyan.Ay = $(this).children('td:eq(5)').html().substring(8, 10);
beyan.Yil = $(this).children('td:eq(5)').html().substring(11, 16);
secililer.push(beyan);
}
});
$.ajax({
url: '/Ebeyan/BeyanAl',
type: "POST",
dataType: "string",
async: false,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ secililer, islemler, token }),
success: function (data) {
$("#mesaj").html(data);
alert("done.");
}
});
});
The controller is here. I have to use Thread.Sleep (1000) in the method. because the server on which I want to download files wants 1 second to pass between each request.
public async Task<string> BeyanAl(List<Beyanname> secililer, List<Islem> islemler, string token)
{
bool indir = true;
bool yazdir = false;
bool gonder = false;
foreach (var islem in islemler)
{
if (islem.IslemTuru =="cbyazdir")
{
yazdir = true;
}
if (islem.IslemTuru == "cbgonder")
{
gonder= true;
}
}
foreach (var GelenBeyan in secililer)
{
string YolAdi = YolHazirla(GelenBeyan);
string DosyaAdi = DosyaAdiHazirla(GelenBeyan);
await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Beyan", GelenBeyan.Id, "");
await dosyaindir(token, YolAdi + "/" + DosyaAdi, "Tahakkuk", GelenBeyan.Id, GelenBeyan.TahakkukId);
}
return "İndirildi";
}
here is chrome response screens
r1
r2
There is no 'string' data type in ajax datatypes make it json or text
$.ajax({
url: '/Ebeyan/BeyanAl',
type: "POST",
dataType: "string", <-- make it json or text
async: false,
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ secililer, islemler, token }),
success: function (data) {
$("#mesaj").html(data);
alert("done.");
}
});
I have an asp.net mvc application and want to upload files and form data with ajax and also want to use [ValidateAntiForgeryToken]-Attribute. But i not want use formData class client side (because of brower support).
My client code:
function sendMessage(id) {
if (window.FormData !== undefined) { //
var fileData = new FormData();
fileData.append('profileId', id);
fileData.append('title', $('#Message_Title').val());
fileData.append('text', $('#Message_Text').val());
fileData.append('__RequestVerificationToken', $('[name=__RequestVerificationToken]').val());
var files = $("#Message_Image").get(0).files;
if (files[0]) {
fileData.append(files[0].name, files[0]);
}
$.ajax({
url: '/Manage/SendMessage',
type: "POST",
contentType: false,
processData: false,
data: fileData,
success: function (result) {
alert(result);
},
error: function (err) {
alert(err.statusText);
}
});
} else {
var files2 = $("#Message_Image").get(0).files;
$.ajax({
url: '/Manage/SendMessage',
type: 'POST',
//contentType: false,
//processData: false,
data: {
profileId: id,
title: $('#Message_Title').val(),
text: $('#Message_Text').val(),
//image: files2[0], <--- IF I UNCOMMENT THIS IT NOT WORKS ANYMORE
__RequestVerificationToken: $('[name=__RequestVerificationToken]').val()
},
success: function (result) {
},
error: function (err) {
alert(err.statusText);
}
});
}
};
Server code:
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult SendMessage(int? profileId, string title, string text)
{
HttpPostedFileBase file = Request.Files["Image"];
HttpFileCollectionBase files = Request.Files;
return Json(null, "text/html");
}
I tried a bunch of stuff and this is what i have so far:
So i try to call this method "runTest()" from my javascript.
The method does not actually go, and it goes through as a success, so the "handleTest()" goes and also succeeds. I need it to actually call the controller function.
Javascript/Angular/JQuery
$scope.runTest = function (myTest) {
$.ajax({
method: 'POST',
Url: 'Tests/runTest',
contentType: 'application/json;',
data: myTest,
success: function () { handleTest(myTest); },
error: function () { alert('no object found'); }
});
}
function handleTest(currentTest){
var updated = { id: currentTest.id, name: currentTest.name, schedule: currentTest.schedule, description: currentTest.description, server: currentTest.server, port: currentTest.port, method: currentTest.method };
//var updated = currentTest;
$http({
method: "PUT",
url: 'http://~~api address~~/api/tests/' + (currentTest.id),
data: updated
})
.success(function (data, status, headers) {
alert("Test was successfully updated.");
$state.reload();
})
.error(function (data, status, headers) {
alert("Test could not be updated.");
$state.reload();
});
}
C# controller method
(named TestsController)
[HttpPost]
public ActionResult runTest(StandardTest myTest)
{
myTest.lastResult = MyEnum.Pass;
log.Info(myTest.name + " " + myTest.lastResult + " " + myTest.id);
return Json(myTest, JsonRequestBehavior.AllowGet);
}
Any help would be so so appreciated.
An example could be as follows:
In your c# controller
[HttpPost]
public ActionResult runTest(StandardTest myTest)
{
myTest.lastResult = MyEnum.Pass;
log.Info(myTest.name + " " + myTest.lastResult + " " + myTest.id);
if (!testPassed){
//test did not pass
return Json(new {success = false,
responseText = "Test did not pass",JsonRequestBehavior.AllowGet);
}
else
{
//Test Passed
return Json(new {success = true,
responseText= "Test Passed"},JsonRequestBehavior.AllowGet);
}
}
Try to run your web request using the angular $http service.
angular.module('YourModuleName')
.controller("ngControllerName", ["$http", "$scope", function ($http, $scope) {
/*Request to C# Controller*/
$scope.runTest = function(myTest){
var config = {
params:{myTest: myTest}
}
$http.get('/Tests/runTest', config).success(function (data) {
if(data !=null && data.success){
handleTest(myTest);
}
}).error(function (error) {
//Handle Error
});
}
}
I have issue with sending object contains array to a controller
this is my js code
var messageId = 0;
function DraftMessage()
{
var to = [];
var i = 0;
$('#to option:selected').each(function (index, element) {
to[i++] = $(element).val();
});
console.log(to);
$.ajax({
type: "POST",
url: "#Url.Action("DraftMessage", "Activities")",
datatype: "json",
traditional: true,
async: false,
data: { "id": messageId, "To": to, "Title": $("#title").val(), "Project": $("#project").val(), "AreaId": $("#areaId").val(), "Body": $("#messageBody").val() },
beforeSend: function () { }
}).done(function (Id) {
console.log(Id);
messageId = Id;
});
}
$("input, select, textarea").change(function () { DraftMessage(); });
var contents = $('.note-editable').html();
$(".compose-message").on("blur", ".note-editable", function () {
if (contents != $(this).html()) {
DraftMessage();
contents = $(this).html();
}
});
and this is my controller side
public int DraftMessage(message draftMessage, HttpPostedFileBase[] files = null)
{
return new MessageActions().DraftMessage(draftMessage);
}
my issue is that the ajax request always send the to array as null, I do not know what is wrong so could anyone help me to resolve this issue.
Can you change your request and use
dataType: "json",
contentType: "application/json;charset=utf-8",
This should work. Please let me know.
Try this. Push your object to array and send it as Json.
array.push({yourobject datas here})
$.ajax({
type: "POST",
url: '/DraftMessage/Activities',
contentType: 'application/json',
data: JSON.stringify(array),
success: function (d) {
..
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
Convert your controller function's return type to JSonResult.
Hope helps.
do you want upload file using ajax ?!!
use the normal usage of form not the Ajax.BeginForm then in form submit event
write your code like this:
$('#Form').submit(function () {
var xhr = new XMLHttpRequest();
var fd = new FormData();
var file = $('#Image').val();
if (file) {
var fname = $('#Image')[0].files[0].name;
if (CheckFile(file)) {
var uploadFile = document.getElementById('Image').files[0];
var myArray = [];
myArray.push(uploadFile);
if (myArray.length > 0) {
for (var i = 0; i < myArray.length; i = i + 1) {
fd.append("File1", myArray[i]);
}
}
}
else {
return false;
}
}
fd.append("ID", messageId);
fd.append("Title", $('#Title').val());
fd.append("Project", $('#Project').val());
fd.append("AreaId", $('#AreaId').val());
fd.append("Body", $('#messageBody').val());
var form = $('#Form');
var token = $('input[name="__RequestVerificationToken"]', form).val();
fd.append("__RequestVerificationToken", token);
xhr.open("POST", "/ControllerName/Action/", true);
xhr.send(fd);
xhr.addEventListener("load", function (event) {
if (event.target.response != "OK") {
OnFail(event.target.response);
}
else {
OnSuccess(event);
}
}, false);
return false;
})
server side in controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult actionName(Model pModel){
HttpPostedFileBase File = Request.Files["File1"];
if (File != null && File.ContentLength != 0){
//do what you want
return Content("OK");
}
else{
Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
return Content("Error Messages", System.Net.Mime.MediaTypeNames.Text.Plain);
}
}
You can try a different approach. You can serialize your entire form by doing something like this:
var formdata = $("#frmEmailInfo").serialize();
and then post it to the Controller:
$.ajax(
{
type: "POST",
data: formdata,
dataType: 'json',...
I am trying to create a progree update module when users save some data from my html5 front end. I have the following code but it does not seem to work. I just don't get any percentcompleted values. Any ideas why this might be failing.
The webservices are c# asmx services.
$.ajax({
xhr: function()
{
if (window.ActiveXObject) {
return new window.ActiveXObject("Microsoft.XMLHTTP");
}
else {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
alert(percentComplete);
}
}, false);
xhr.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log(percentComplete);
alert(percentComplete);
}
}, false);
return xhr;
}
},
type: "POST",
url: "../service.asmx/SaveSession",
cache: false,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(DTO),
dataType: "json",
async:false,
success: function (response) {
result = response;
IsDataDirty = false;
},
});
use code below :
{
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if (xhr.upload) {
xhr.upload.addEventListener('progress', function(e) {
var percentage = event.loaded / event.total * 100;
//.....
}, false);
}
return xhr;
});
}