Exception
Exception

Reputation: 8389

Input type=file alternative to be used in Google chrome extension

I am developing a Google chrome extension where user uploads a file manually. I have created a file type input in popup.html and tried to read the file. But that is not working as that is bug in Google chrome..

Please someone suggest me if there is any other work around. I found this but I am unable to proceed cause I could not understand their terminology.

Thanks

Upvotes: 0

Views: 1266

Answers (1)

Exception
Exception

Reputation: 8389

I could not find any workaround to solve this problem and solved it in another way..Here I am posting the code which I have used

First in Popup Page :

 chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.sendRequest(tab.id, {
                'action': 'inject'
      });
 }); 

In Content Script :

 chrome.extension.onRequest.addListener(function ListeningMethod(request, sender,callback)
 {
switch(request.action)
{
    case "inject":
            if($('body').find('#TesterItems').size() < 1)
            {
                $('body').prepend("<div id='TesterItems'><center><input type='file' id='xfilex'></center></div>");
            }
            else
            {
                $('#TesterItems').html("<center><input type='file' id='xfilex'></center>");
                $('#TesterItems').slideToggle('slow');
            }
    break;

 }
});
$('#xfilex').live('change',function()
{
var file = document.getElementById('xfilex').files[0];
if(file)
{
    var reader;
    reader = new FileReader();
    reader.readAsText(file, "UTF-8");
    reader.onload = loaded;
}
});

function loaded(evt)
{
var fileString = evt.target.result;
var str = fileString;
if(str.length > 0 )
{
    chrome.extension.sendRequest({method: "passThisXML", key: str}, function(response) {
        $('#TesterItems').html('<center><span style="padding:3px">Successfully loaded XML.</span></center>');
        $('#TesterItems').slideToggle('slow');
        var Dat = response.data;
    });
}
 }

Upvotes: 1

Related Questions