Jquery function is not working in a synchronus order The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceChrome and Safari ajax issue with async:falseIs there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?Disable/enable an input with jQuery?How can I refresh a page with jQuery?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
How did passengers keep warm on sail ships?
Sort list of array linked objects by keys and values
He got a vote 80% that of Emmanuel Macron’s
How many people can fit inside Mordenkainen's Magnificent Mansion?
Grover's algorithm - DES circuit as oracle?
Was credit for the black hole image misattributed?
Can withdrawing asylum be illegal?
Empty set is subset of every set? If yes, why that...
Semisimplicity of the category of coherent sheaves?
How are presidential pardons supposed to be used?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Am I ethically obligated to go into work on an off day if the reason is sudden?
How is simplicity better than precision and clarity in prose?
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
Working through the single responsibility principle (SRP) in Python when calls are expensive
How to copy the contents of all files with a certain name into a new file?
Is every episode of "Where are my Pants?" identical?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Did the new image of black hole confirm the general theory of relativity?
How do you keep chess fun when your opponent constantly beats you?
Who or what is the being for whom Being is a question for Heidegger?
Hopping to infinity along a string of digits
How can I protect witches in combat who wear limited clothing?
How to grep and cut numbes from a file and sum them
Jquery function is not working in a synchronus order
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceChrome and Safari ajax issue with async:falseIs there an “exists” function for jQuery?Add table row in jQueryHow do I check if an element is hidden in jQuery?Setting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?Disable/enable an input with jQuery?How can I refresh a page with jQuery?jQuery scroll to element“Thinking in AngularJS” if I have a jQuery background?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have two jQuery functions for image upload and saved in to the path as well a check of image upload size and height and width. Both functions work fine, but not in a synchronous way. It jumps out on next function without getting the result of other one.
Whenever I upload an image, the "checkheightwidth" function will always return false
.
What's gong wrong?
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
jquery
|
show 1 more comment
I have two jQuery functions for image upload and saved in to the path as well a check of image upload size and height and width. Both functions work fine, but not in a synchronous way. It jumps out on next function without getting the result of other one.
Whenever I upload an image, the "checkheightwidth" function will always return false
.
What's gong wrong?
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
jquery
1
Useasync:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514
– freedomn-m
Mar 22 at 6:46
The problem with your code ischeckheightwidth
function doesn't return anything as you have commented//return true;
. So it will returnundefined
. That's why you're gettingfalse
inif condition
.
– Karan
Mar 22 at 6:46
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09
|
show 1 more comment
I have two jQuery functions for image upload and saved in to the path as well a check of image upload size and height and width. Both functions work fine, but not in a synchronous way. It jumps out on next function without getting the result of other one.
Whenever I upload an image, the "checkheightwidth" function will always return false
.
What's gong wrong?
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
jquery
I have two jQuery functions for image upload and saved in to the path as well a check of image upload size and height and width. Both functions work fine, but not in a synchronous way. It jumps out on next function without getting the result of other one.
Whenever I upload an image, the "checkheightwidth" function will always return false
.
What's gong wrong?
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails())
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
else
//alert('something went wrong');
console.log('ss');
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="UploadImg" />
<br />
<p id="fileInfo"></p>
<img id="imgPreview" class="thumbnail" style="height:200px; width:200px" />
<div class="input-group">
<span class="input-group-btn">
<button class="btn" type="button" data-clipboard-target="#txtImg" onclick="copytxt();">
<i class="fa fa-clipboard" aria-hidden="true"></i>
</button>
</span>
<input type="text" placeholder="Copy Image link" id="txtImg" class="form-control" aria-label="..." onchange="copytxt();" />
</div>
jquery
jquery
edited Mar 22 at 6:39
showdev
20.4k133352
20.4k133352
asked Mar 22 at 6:32
miteshmitesh
34
34
1
Useasync:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514
– freedomn-m
Mar 22 at 6:46
The problem with your code ischeckheightwidth
function doesn't return anything as you have commented//return true;
. So it will returnundefined
. That's why you're gettingfalse
inif condition
.
– Karan
Mar 22 at 6:46
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09
|
show 1 more comment
1
Useasync:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514
– freedomn-m
Mar 22 at 6:46
The problem with your code ischeckheightwidth
function doesn't return anything as you have commented//return true;
. So it will returnundefined
. That's why you're gettingfalse
inif condition
.
– Karan
Mar 22 at 6:46
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09
1
1
Use
async:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514– freedomn-m
Mar 22 at 6:46
Use
async:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514– freedomn-m
Mar 22 at 6:46
The problem with your code is
checkheightwidth
function doesn't return anything as you have commented //return true;
. So it will return undefined
. That's why you're getting false
in if condition
.– Karan
Mar 22 at 6:46
The problem with your code is
checkheightwidth
function doesn't return anything as you have commented //return true;
. So it will return undefined
. That's why you're getting false
in if condition
.– Karan
Mar 22 at 6:46
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09
|
show 1 more comment
1 Answer
1
active
oldest
votes
You have to move your code upload file code to some another function like uploadFile
below. And then call this function from img.onload
before return true
.
Also as per comment from @freedomn-m you should not use async:false
. Even from your code I can see that if you remove async:false
then also it will not make any issue.
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55294070%2fjquery-function-is-not-working-in-a-synchronus-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to move your code upload file code to some another function like uploadFile
below. And then call this function from img.onload
before return true
.
Also as per comment from @freedomn-m you should not use async:false
. Even from your code I can see that if you remove async:false
then also it will not make any issue.
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
add a comment |
You have to move your code upload file code to some another function like uploadFile
below. And then call this function from img.onload
before return true
.
Also as per comment from @freedomn-m you should not use async:false
. Even from your code I can see that if you remove async:false
then also it will not make any issue.
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
add a comment |
You have to move your code upload file code to some another function like uploadFile
below. And then call this function from img.onload
before return true
.
Also as per comment from @freedomn-m you should not use async:false
. Even from your code I can see that if you remove async:false
then also it will not make any issue.
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
You have to move your code upload file code to some another function like uploadFile
below. And then call this function from img.onload
before return true
.
Also as per comment from @freedomn-m you should not use async:false
. Even from your code I can see that if you remove async:false
then also it will not make any issue.
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
var _URL = window.URL || window.webkitURL;
$("#UploadImg").change(function(e)
if (checkFileDetails()) else
//alert('something went wrong');
console.log('ss');
);
function uploadFile()
var data = new FormData();
var files = $("#UploadImg").get(0).files;
if (files.length > 0)
data.append("MyImages", files[0]);
$.ajax(
url: "/Image/UploadFile",
type: "POST",
processData: false,
contentType: false,
data: data,
async: false,
success: function(response)
//code after success
$("#txtImg").val('/Upload/' + response);
$("#imgPreview").attr('src', '/Upload/' + response);
,
error: function(er)
alert(er);
);
function checkFileDetails()
var fi = document.getElementById('UploadImg');
if (fi.files.length > 0) // FIRST CHECK IF ANY FILE IS SELECTED.
for (var i = 0; i <= fi.files.length - 1; i++)
// GET THE IMAGE WIDTH AND HEIGHT USING fileReader() API.
function checkheightwidth(imgfile)
var file, img;
if ((file = imgfile))
img = new Image();
img.onload = function()
alert(this.width + " " + this.height);
var w = this.width;
var h = this.height;
if (w < 1024 && h < 790)
alert('Please upload high resolution image.');
$('#UploadImg').val('');
return false;
else
uploadFile();
return true;
;
img.src = _URL.createObjectURL(file);
//return true;
answered Mar 22 at 6:54
KaranKaran
3,4742525
3,4742525
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55294070%2fjquery-function-is-not-working-in-a-synchronus-order%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use
async:false
is evil, mmmkay: stackoverflow.com/a/41756658/2181514– freedomn-m
Mar 22 at 6:46
The problem with your code is
checkheightwidth
function doesn't return anything as you have commented//return true;
. So it will returnundefined
. That's why you're gettingfalse
inif condition
.– Karan
Mar 22 at 6:46
Hi Karan, Thanks for answer , but if I uncomment return true so in any of the case it will true which is not workable also. I have to restrict image of lower resolution
– mitesh
Mar 22 at 6:55
You can check my answer. Hopefully it will work for you.
– Karan
Mar 22 at 6:59
Sure Karan, I will check this and thank you for your help
– mitesh
Mar 22 at 7:09