js:输入文件到json,例如JSON.stringify [英] js: input file to json with for example JSON.stringify

查看:225
本文介绍了js:输入文件到json,例如JSON.stringify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将html输入文件转换为json字符串,如下所示:

  var jsonString = JSON.stringify(file ); 
console.log(file);
console.log(jsonString);

现在,在我的萤火虫中:

  File {size = 360195,type =image / jpeg,name =xyz.jpg,mehr ...} 
Object {}

为什么 jsonString 是空的?



背景信息:我想用jsonp发送文件引用到另一个php服务器

附加信息:我只想转换文件指针(引用)传递给一个字符串,通过GET发送它。 c $ c>与 File API (convert chrome firefox safari 浏览器不起作用(将文件对象转换为 {} )[我做过不知道原因]



您可以使用<$将文件对象转换为字符串c $ c> JSON.Stringify



例:

  //获取文件对象
var fileObject = getFile();

//重新创建新对象并将文件数据设置到其中
var newObject = {
'lastModified':fileObject.lastModified,
'lastModifiedDate':fileObject.lastModifiedDate ,
'name':fileObject.name,
'size':fileObject.size,
'type':fileObject.type
};

//然后在新对象上使用JSON.stringify
JSON.stringify(newObject);

另一种解决方案
你可以添加 toJSON()行为给你的 File object



EX:

  //获取文件对象
var fileObject = getFile();

//实现toJSON()行为
fileObject.toJSON = function(){return {
'lastModified':myFile.lastModified,
'lastModifiedDate':myFile .lastModifiedDate,
'name':myFile.name,
'size':myFile.size,
'type':myFile.type
};}

//然后在File对象上使用JSON.stringify
JSON.stringify(fileObject);

注释:send File 使用 POST的服务器对象动词


I want to convert a html input file to a json string like this:

var jsonString = JSON.stringify(file);
console.log( file );
console.log( jsonString );

Now, in my firebug:

File { size=360195, type="image/jpeg", name="xyz.jpg", mehr...} 
Object {}

Why is the jsonString empty?

Background info: I want to send the file-reference with jsonp to another php server

Additional Information: I want to convert only the file-pointer (reference) to a string, to send it via GET.

解决方案

JSON.Stringify with File API (convert File object to string) in chrome, firefox and safari browser does not work (convert File Object to {}) [ I Did not know the reason for that]

You can make a work around to convert File object to string using JSON.Stringify

Ex:

// get File Object  
var fileObject = getFile();

// reCreate new Object and set File Data into it
var newObject  = {
   'lastModified'     : fileObject.lastModified,
   'lastModifiedDate' : fileObject.lastModifiedDate,
   'name'             : fileObject.name,
   'size'             : fileObject.size,
   'type'             : fileObject.type
};  

// then use JSON.stringify on new object
JSON.stringify(newObject);

Another Solution: you can add toJSON() behavior to your File object

EX:

   // get File Object  
    var fileObject = getFile();

    // implement toJSON() behavior  
    fileObject.toJSON = function() { return {
       'lastModified'     : myFile.lastModified,
       'lastModifiedDate' : myFile.lastModifiedDate,
       'name'             : myFile.name,
       'size'             : myFile.size,
       'type'             : myFile.type 
    };}  

    // then use JSON.stringify on File object
    JSON.stringify(fileObject);

Notes: send File Object to server using POST verb

这篇关于js:输入文件到json,例如JSON.stringify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆