Get file url in javascript

--UPDATE--

So I am attempting to create a file previewer that allows someone to upload files with on the front end, access the browser files as a blob, and preview them in an iframe.

MUST BE ABLE TO PREVIEW ALL OPEN DOCUMENT FILES

My current issue is that viewer.js (http://viewerjs.org/instructions/) doesn't seem to work with blob files. This was the closest information I got..https://github.com/kogmbh/ViewerJS/issues/230

Any ideas on a way to have this work with all open document files? Plugin recommendations?

Current Code below..

    fileUploadProcessFiles: function(fileInput){
            console.log("MODALJS.fileUploadProcessFiles");
            var m = $(document).find("#modal"),
                list = $("#uploadList"),
                files = fileInput.files,
                type = m.find("option:selected").text();

            for (var i = 0; i < files.length; i++) {
                // Display List
                list.append(`
"+ "
`); // Store Preview Links var blobURL = URL.createObjectURL(files[i]); MODALJS.fileUploadPreviewLinks.push(blobURL); // Append Iframe Preview list.append(``); // Push to upload queue MODALJS.fileUploadFiles.push(["file", files[i]]); } },

--UPDATE #2--

So I got it figured out. I had to use a different plugin. webODF instead... I should be able to cobble together a decent enough solution now.

fileUploadProcessFiles: function(fileInput){
            console.log("MODALJS.fileUploadProcessFiles");
            var m = $(document).find("#modal"),
                list = $("#uploadList"),
                files = fileInput.files,
                type = m.find("option:selected").text();

            for (var i = 0; i < files.length; i++) {
                // Display List
                list.append(`
"+ "
`); // Store Preview Links var blobURL = URL.createObjectURL(files[i]); MODALJS.fileUploadPreviewLinks.push(blobURL); // Append Iframe Preview list.append(`
`); odfElement = document.getElementById("odfCanvas"); odfcanvas = new odf.OdfCanvas(odfElement); odfcanvas.load(blobURL); // Push to upload queue MODALJS.fileUploadFiles.push(["file", files[i]]); } },

This snippet will get the filename from the url. The filename is the last part of the URL from the last trailing slash. For example, if the URL is http://www.example.com/dir/file.html then file.html is the file name.

Explanation

var url = window.location.pathname;

This declares the url variable and adds the current pathname as its value.

var filename = url.substring(url.lastIndexOf('/')+1);
alert(filename);

substring (method) - extract characters from start (parameter). url is the stringObject url.substring(start)

lastIndexOf (method) - position of last occurrence of specified string value, in this case the ‘/’

Add one to lastIndexOf because we do not want to return the ‘/’

Full snippet

var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
alert(filename);

The URL.createObjectURL() static method creates a string containing a URL representing the object given in the parameter.

The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.

To release an object URL, call revokeObjectURL().

Note: This feature is available in Web Workers

Note: This feature is not available in Service Workers due to its potential to create memory leaks.

Syntax

Parameters

object

A File, Blob, or MediaSource object to create an object URL for.

Return value

A string containing an object URL that can be used to reference the contents of the specified source object.

Examples

Usage notes

Memory management

Each time you call createObjectURL(), a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them.

Browsers will release object URLs automatically when the document is unloaded; however, for optimal performance and memory usage, if there are safe times when you can explicitly unload them, you should do so.

Using object URLs for media streams

In older versions of the Media Source specification, attaching a stream to a element required creating an object URL for the MediaStream. This is no longer necessary, and browsers are removing support for doing this.

Warning: If you still have code that relies on createObjectURL() to attach streams to media elements, you need to update your code to set srcObject to the MediaStream directly.

Specifications

Specification
File API
# dfn-createObjectURL

Browser compatibility

BCD tables only load in the browser

See also

How do I get the URL of a JavaScript file?

To retrieve just the URL as a string, the read-only document. URL property can be used. Sun reference: Do not use location as a property of the document object; use the document. URL property instead.

How do I find the URL for an uploaded file?

Answers. string UploadDir=Server. MapPath("~/UploadFolder"); strFileName = Path. Combine( UploadDir,FileUpload1.

What is Objecturl?

Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.

How do you split a URL?

var newURL="http://www.example.com/index.html/homePage/aboutus/"; console. log(newURL); var splitURL=newURL. toString(). split("/"); console.