hello I want to know how to connect this web app script to upload files from url to web component
function uploadFiles(url) {
var response = UrlFetchApp.fetch(url)
var fileName = getFilenameFromURL(url)
var folder = DriveApp.getFolderById('1E_3885uoAUKNeW2ESsw--Lp3lYFQJvhV');
var blob = response.getBlob();
var file = folder.createFile(blob)
file.setName(fileName)
file.setDescription("Download from the " + url)
return file.getUrl();
}
function getFilenameFromURL(url) {
//(host-ish)/(path-ish/)(filename)
var re = /^https?:\/\/([^\/]+)\/([^?]*\/)?([^\/?]+)/;
var match = re.exec(url);
if (match) {
return unescape(match[3]);
}
return null;
}
function doGet(e){
var html = HtmlService.createHtmlOutputFromFile('index.html')
return html.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
}
You will (currently) need to use an extension (CustomWebView) to facilitate handling of an input = file tag in your html. (however, because you are calling files from urls, it may work in a webviewer)
You run it by calling the scriptUrl in a webviewer
function doGet(e) {
return myFunction(e.parameter.newUrl,e.parameter.mimetype);
}
function myFunction(newUrl,mimetype) {
var url = newUrl;
var response = UrlFetchApp.fetch(url);
var blob=response.getAs(mimetype);
var folder = "1DguuZolc10bsW1XP0y7QYVY1rPWSHKxm";
var fileName=decodeURIComponent(url.split("/").pop());
var file=DriveApp.getFolderById(folder).createFile(blob);
file.setName(fileName);
return ContentService.createTextOutput("File from Url uploaded")
}
@TIMAI2
ohh thank you very much you have just made my day and I added function to the script that get google drive URL after uploading the URL the script will be like this :
function doGet(e) {
return myFunction(e.parameter.newUrl,e.parameter.mimetype);
}
function myFunction(newUrl,mimetype) {
var url = newUrl;
var response = UrlFetchApp.fetch(url);
var blob=response.getAs(mimetype);
var folder = "1DguuZolc10bsW1XP0y7QYVY1rPWSHKxm";
var fileName=decodeURIComponent(url.split("/").pop());
var file=DriveApp.getFolderById(folder).createFile(blob);
file.setName(fileName);
return ContentService.createTextOutput("URL:" + file.getUrl());
}