If I have a URL to a video file, how can I detect if the resource pointed by the URL is valid and exists before it can be displayed? I've seen some answers suggesting AJAX, but I only know AJAX to send and retrieve some data, not to get the status of the file whether it exists or not.
For example, if I have a URL like http://www.example.com/video.mp4
, how could I check whether video.mp4
exists or not and can or cannot be retrieved?
In the latest Field Service v8.8.x/Universal Resource Scheduling v3.8.x update, we released the Search Resource Availability API for scheduling single requirements. A list of available resources and their time slots can be retrieved by passing the details of a single resource requirement to this API as input.
The Search Resource Availability for Requirement Group API returns available time slots for resources when you search by using requirement groups. The Create Requirement Group Bookings API uses the available time slots for resources to generate booking records for your requirement groups.
Applies to: Dynamics 365 Organization 9.0+ with Universal Resource Scheduling 3.8.x version In the latest Field Service v8.8.x/Universal Resource Scheduling v3.8.x update, we released the Search Resource Availability API for scheduling single requirements.
Resource availability refers to the information about what resources you can use to service projects, when, and under what conditions. This information is critical, because on every project, the ability to succeed depends on whether you have access to the essential resources and tools.
You don't really need ajax, just create a video element, and see if it can load the source
var video = document.createElement('video');
video.onload = function() {
alert('success, it exist');
// show video element
}
video.onerror = function() {
alert('error, couldn\'t load');
// don't show video element
}
video.src = 'http://www.example.com/video.mp4';
Different browsers play different formats, to check if the file can be played in the current browser, you can use the canplaythrough
event
video.oncanplaythrough = function() {
alert("This file can be played in the current browser");
};
if the file is on the same domain, and ports and protocol match, you can use ajax to do a HEAD request and see if the resource exists, but that won't work cross-domain
var http = new XMLHttpRequest();
http.open('HEAD', '/folder/video.mp4');
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
if (this.status != 404) {
// resource exists
}
}
};
http.send();
You can send a HEAD
request. HEAD
requests send back only the HTTP headers, so if you get a status of 200 or 304 it means the resource exists, if you get a 404 it means the resource doesn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With