How do I check if a file on my server exists in jQuery or pure JavaScript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
- jQuery add and remove delay
This works for me:
For a client computer this can be achieved by:
This is my program to transfer a file to a specific location and shows alert if it does not exist
With jQuery:
EDIT:
Here is the code for checking 404 status, without using jQuery
Small changes and it could check for status HTTP status code 200 (success), instead.
So long as you're testing files on the same domain this should work:
Please note, this example is using a GET request, which besides getting the headers (all you need to check weather the file exists) gets the whole file. If the file is big enough this method can take a while to complete.
The better way to do this would be changing this line:
req.open('GET', url, false);
toreq.open('HEAD', url, false);
It works for me, use iframe to ignore browsers show GET error message
This doesn't address the OP's question, but for anyone who is returning results from a database: here's a simple method I used.
If the user didn't upload an avatar the
avatar
field would beNULL
, so I'd insert a default avatar image from theimg
directory.then