【问题标题】:ReadAsDataURL() doesn't workReadAsDataURL() 不起作用
【发布时间】:2013-11-02 09:19:54
【问题描述】:

我试图在我的网站中实现拖放功能。 我需要将拖放的图像转换为数据 URI,以便我可以将它与 JCrop 一起使用并在之后上传。

            drop: function (e) {
                e = e || window.event;
                e.preventDefault();
                e = e.originalEvent || e;           
                var files = (e.files || e.dataTransfer.files);
                var reader = new FileReader();
                reader.onload = function (event){
                    console.log(event.target.result);
                };
                reader.readAsDataURL(files);
                return false;
            }

但控制台中没有显示任何内容。甚至没有undefinedfiles 变量返回一个带有我删除的图像的 Object FileList,所以问题不存在。我怎样才能解决这个问题? :(

【问题讨论】:

  • 你能解决这个问题吗?

标签: javascript jquery html fileapi


【解决方案1】:
//applies to only one file.
reader.readAsDataURL(files); 

解决方案:

for(var i=0;i<files.length;i++){
   reader.readAsDataURL(files[i]);
}

【讨论】:

  • 非常感谢,当我将引号替换为分号时它可以工作:)
  • 只是输入错误。一点也不)
【解决方案2】:

你可以试试这个

HTML

<div id="dropBox">
 <div>Drop your image here...</div>
</div>

CSS

#dropBox {
  margin: 15px;
  width: 300px;
  height: 300px;
  border: 5px dashed gray;
  border-radius: 8px;
  background: lightyellow;
  background-size: 100%;
  background-repeat: no-repeat;
  text-align: center;
  }

#dropBox div {
  margin: 100px 70px;
  color: orange;
  font-size: 25px;
  font-family: Verdana, Arial, sans-serif;
  } 

JavaScript

var dropBox ;

window.onload = function() 
{
 dropBox = document.getElementById("dropBox");
 dropBox.ondrop = drop;
};

function drop(e)
{
  // Get the dragged-in files.
  var data = e.dataTransfer;
  var files = data.files;

 // Pass them to the file-processing function.
  processFiles(files);
}

function processFiles(files)
{
  var file = files[0];

 // Create the FileReader.
 var reader = new FileReader();

 // Tell it what to do when the data URL is ready.
  reader.onload = function (e) 
  {
    // Use the image URL to paint the drop box background
    dropBox.style.backgroundImage = "url('" + e.target.result + "')";
  };

 // Start reading the image.
 reader.readAsDataURL(file);
}

【讨论】:

    【解决方案3】:
    For FileReader to read contents of file correctly such word documents and excelsheets, the contentType may also have to be specified :-
    The controller class :-
    public class SagarAttachFile {
        public String fileName {get;set;}
        public String fileValue {get;set;}
        public String contentType {get;set;}
        public Attachment attachment {
            get {
                if(attachment == null)
                    attachment = new Attachment();
                return attachment;
            }
            set;
        }
        public SagarAttachFile() // Constructor
        {
            fileName = '';
            fileValue = '';
            contentType = '';
        }
        public PageReference saveMethod()
        {
            if(attachment != null && fileName != null && fileName != ''
                && fileValue != null && fileValue != '')
            {        
                try
                {
                    attachment.ownerId = UserInfo.getUserId();
                    attachment.ParentId = '5002C000006zvjyQAA'; // Attach the uploaded file as an attachment to this Case.
                    attachment.Name = fileName;       
                    fileValue = EncodingUtil.urlDecode(fileValue, 'UTF-8');
                    attachment.Body = EncodingUtil.base64Decode(fileValue);
                    attachment.ContentType = contentType;
                    insert attachment;
                }
                catch (DMLException e) {
                    System.debug(LoggingLevel.INFO, '#### error occurred while adding attachment to case ' + e.getMessage());
                }
                finally
                {
                    attachment = new Attachment(); 
                }
            }
            else
            {
                System.debug(LoggingLevel.INFO, '#### no attachment adding to case');
            }
            return null;
        }           
    }
    The Visualforce page :- browse for by appending this to URL :- apex/SagarAttachFileVF
    <apex:page controller="SagarAttachFile">
        <apex:form>
            <apex:actionFunction name="saveAF" action="{!saveMethod}" reRender="attchFilePanel">
                <apex:param assignTo="{!fileName}" value="" name="fileName"/>
                <apex:param assignTo="{!fileValue}" value="" name="fileValue"/>
                <apex:param assignTo="{!contentType}" value="" name="contentType"/>
            </apex:actionFunction>
            <apex:pageBlock id="pageBlock1">
                <apex:outputPanel id="attchFilePanel">
                    <input type="file" id="file" name="attFile"/>
                    <apex:commandButton value="Save" onclick="javascriptFunc1(); return false;"/>
                </apex:outputPanel>
            </apex:pageBlock>
        </apex:form>
        <script>
        function javascriptFunc1()
        {
            var fileName = null;
            var file1 = document.getElementById('file');   
            if(file1 != null)
            {
                if ('files' in file1)
                {
                    if (file1.files.length == 1)
                    {
                        alert('one file attached');
                        var file1Obj = file1.files[0];                           
                        if(file1Obj.name != '')
                        {
                            fileName = file1Obj.name;
                        }
                        var reader = new FileReader();
                        reader.onload = function() {
                            alert('reading is done now');
                            var fileContent = reader.result;
                            var base64 = 'base64,';
                            var dataStart = fileContent.indexOf(base64) + base64.length;
                            fileContent = fileContent.substring(dataStart);
                            var encodedFileContent = encodeURIComponent(fileContent);
                            saveAF(fileName, encodedFileContent, file1Obj.type);                        
                        }
                        reader.readAsDataURL(file1Obj);    
                    }
                    else
                    {
                        alert('no file attached');    
                    }
                }    
            }
        }
        </script>
    </apex:page>
    

    【讨论】:

      猜你喜欢
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多