I'm writing an extension for Chrome, and I need to upload a file from the page the user is currently on to my server to be processed, I cannot figure out how to upload the file though. I considered just passing the link to the server and having the server download the file, however if the site requires authentication this will not work. Is it possible to upload a file via a Chrome extension to my server?
In Chrome, go to the "Menu" icon, then choose "Settings." 3. Scroll to the bottom and select "Advanced." 4. In the "Privacy and Security" section, select "Content settings." 5. Scroll down and select "PDF documents," then switch it to "On."
(Chrome OS only) Extends Chrome. For example, apps or extensions can allow users to upload files to a website. Allows app or extension to create, read, navigate, and write to the user's local file system at a user-selected location.
I've recently developed a Chrome extension which retrieves content from a page, and sends it to the server.
The following approach was used:
src
property of an <img>
element, for example.XMLHttpRequest
from the background page.Side note, to take the checksum of the image, Crypto-JS: MD5 can be used. Example (where xhr
is the XMLHttpRequest
object with responseType
set to arraybuffer
, see Worker demo):
var md5sum = Crypto.MD5( new Uint8Array(xhr.response) );
// Example: Grab the first <img> from the document if it exists. var img = document.images[0]; if (img) { // Send the target of the image: chrome.runtime.sendMessage({method: 'postUrl', url: img.src}); }
chrome.runtime.onMessage.addListener(function(request) { if (request.method == 'postUrl') { var worker = new Worker('worker.js'); worker.postMessage(request.url); } });
// Define the FormData object for the Web worker: importScripts('xhr2-FormData.js') // Note: In a Web worker, the global object is called "self" instead of "window" self.onmessage = function(event) { var resourceUrl = event.data; // From the background page var xhr = new XMLHttpRequest(); xhr.open('GET', resourceUrl, true); // Response type arraybuffer - XMLHttpRequest 2 xhr.responseType = 'arraybuffer'; xhr.onload = function(e) { if (xhr.status == 200) { nextStep(xhr.response); } }; xhr.send(); }; function nextStep(arrayBuffer) { var xhr = new XMLHttpRequest(); // Using FormData polyfill for Web workers! var fd = new FormData(); fd.append('server-method', 'upload'); // The native FormData.append method ONLY takes Blobs, Files or strings // The FormData for Web workers polyfill can also deal with array buffers fd.append('file', arrayBuffer); xhr.open('POST', 'http://YOUR.DOMAIN.HERE/posturl.php', true); // Transmit the form to the server xhr.send(fd); };
FormData
for Web workers POLYFILL Web workers do not natively support the FormData
object, used to transmit multipart/form-data
forms. That's why I've written a polyfill for it. This code has to be included in the Web worker, using importScripts('xhr2-FormData.js')
.
The source code of the polyfill is available at https://gist.github.com/Rob--W/8b5adedd84c0d36aba64
{ "name": "Rob W - Demo: Scraping images and posting data", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": ["http://*/*", "https://*/*"], "js": ["contentscript.js"] } ], "background": { "scripts": ["background.js"] }, "permissions": ["http://*/*", "https://*/*"] }
chrome.runtime.onMessage
Google Chrome Extensions XMLHttpRequest
Level 2 W3c specification FormData
(XHR2) MDN 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