Skip to content

Instantly share code, notes, and snippets.

@jsvini
Created June 12, 2018 20:42
Show Gist options
  • Save jsvini/9bc8dca809c7771a3f8c6683861bccc2 to your computer and use it in GitHub Desktop.
Save jsvini/9bc8dca809c7771a3f8c6683861bccc2 to your computer and use it in GitHub Desktop.
VTEX upload de arquivos no Chrome sem FLASH

Segue um bookmarklet para realizar upload de arquivos sem Flash no CMS do VTEX.

Basta selecionar o código abaixo e arrastar para a barra de favoritos do Chrome, quando entrar na tela de upload do Vtex, clique no link que salvou na barra de favoritos e o botão para selecionar o arquivo vai aparecer.

javascript:var %20bookmarlet=1%3B(function()%7Bvar%20fileExtesion%3D%24(%22%23insertFileExtension%22).val()%2CbtnEnviar%3D%24(%22.href-save-file%22)%2CfileInput%3D%24(%22%23fileInputFilePicker%22)%3BbtnEnviar.attr(%22href%22%2C%22%23%22)%3BbtnEnviar.show()%3BfileInput.show()%3BbtnEnviar.click(function(b)%7Bb.preventDefault()%3Bb%3DfileInput%5B0%5D%3Bif(!b.files%7C%7C1%3Eb.files.length)alert(%22Selecione%20um%20arquivo!%22)%3Belse%7Bvar%20c%3Db.files%5B0%5D%3Bb%3DMath.random().toString(36).substring(2%2C8).toUpperCase()%3Bvar%20d%3D%7Bfolder%3A%22%2Fadmin%2Fuploads%22%7D%3Bd%5Bb%5D%3Dc.name%3Bvar%20e%3Dfunction()%7Bvar%20a%3Dnew%20FormData%3Ba.append(%22Filename%22%2Cc.name)%3Ba.append(%22requestToken%22%2C%24(%22%23fileUploadRequestToken%22).val())%3Ba.append(%22fileext%22%2C%22*.%22%2BfileExtesion)%3Ba.append(%22folder%22%2C%22%2Fuploads%22)%3Ba.append(%22changedFileId%22%2C%22undefined%22)%3Ba.append(%22Upload%22%2C%22Submit%20Query%22)%3Ba.append(%22Filedata%22%2Cc%2Cc.name)%3B%24.ajax(%7Burl%3A%22%2Fadmin%2Fa%2FFilePicker%2FUploadFile%22%2Cdata%3Aa%2Ccache%3A!1%2CcontentType%3A!1%2CprocessData%3A!1%2Ctype%3A%22POST%22%2Csuccess%3Afunction(a%2Cb%2Cc)%7Ba.fileNameInserted%3Falert(a.mensagem)%3Aalert(%22Falha%20ao%20salvar%20arquivo.%22)%3BfileInput.val()%7D%2Cerror%3Afunction(a%2Cb%2Cc)%7Balert(%22Falha%20ao%20salvar%20arquivo.%22)%7D%7D)%7D%3B%24.post(%22%2Fadmin%2Fa%2FFilePicker%2FFileExists%3FchangedFileName%3D%22%2Cd%2Cfunction(a)%7B0%3CObject.keys(a).length%3Fconfirm(%22O%20arquivo%20j%5Cu00e1%20existe%2C%20deseja%20substituir%3F%22)%26%26e()%3Ae()%7D)%7D%7D)%7D)();void(0);

Não suporta multi upload.

Se não quiser utilizar o bookmarklet, basta colar o código fonte abaixo no console do navegador, na tela de upload:

var fileExtesion = $('#insertFileExtension').val();
var btnEnviar = $('.href-save-file');
var fileInput = $('#fileInputFilePicker');

btnEnviar.attr('href', '#');

btnEnviar.show();
fileInput.show();

btnEnviar.click(function(event){
	event.preventDefault();

	var el = fileInput[0];

	if(!el.files || el.files.length < 1){
		alert('Selecione um arquivo!');
		return;
	}

	var file = el.files[0];
	var randKey = Math.random().toString(36).substring(2, 8).toUpperCase();
	var postData = {folder: '/admin/uploads'};
	postData[randKey] = file.name;

	var doUpload = function(){

		var data = new FormData;

		data.append('Filename', file.name);
		data.append('requestToken', $('#fileUploadRequestToken').val() );
		data.append('fileext', '*.' + fileExtesion);
		data.append('folder', '/uploads');
		data.append('changedFileId', 'undefined');
		data.append('Upload', 'Submit Query');
		data.append('Filedata', file, file.name);

	    $.ajax({
	        url         : '/admin/a/FilePicker/UploadFile',
	        data        : data,
	        cache       : false,
	        contentType : false,
	        processData : false,
	        type        : 'POST',
	        success     : function(data, textStatus, jqXHR){
	            if(data.fileNameInserted){
	            	alert(data.mensagem);
	            }else{
	            	alert('Falha ao salvar arquivo.');
	            }
	            fileInput.val();
	        },
	        error : function(data, textStatus, jqXHR){
	        	alert('Falha ao salvar arquivo.');
	        }
	    });
	}


	$.post('/admin/a/FilePicker/FileExists?changedFileName=', postData, function(response){

		if (Object.keys(response).length > 0){
			var substituir = confirm('O arquivo já existe, deseja substituir?');
			if (substituir){
				doUpload();
			}
		}else{
			doUpload();
		}

	});

})
@raphaelcorreaoct
Copy link

Vc me ajudou bastante com esse arquivo JS. Muito obrigado!!
Estou montado uma feature bem legal com node e isso me serviu bastante. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment