我如何序列化jquery中的文件类型输入 [英] How do I serialize file type input in jquery

查看:133
本文介绍了我如何序列化jquery中的文件类型输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前使用Prototype我可以序列化输入类型文件,它给了我正在上传的文件的文件名,但是当我在jQuery中序列化表单时,我刚刚得到输入类型文本,而其他文件不是文件,我该怎么做?

解决方案

在Firebug中花费了几分钟后,实际上有几种方法可以解决这个问题。例如,我可以从fileObject获得文件列表:

  var file = $(#control ).attr( 文件)[0]; 
var fileName = file.fileName;
var fileSize = file.fileSize;

alert(上传:+ fileName +@+ fileSize +bytes);

显然,我可以读取序列化的值。但写作是另一个问题。

但显然这不像其他人声称的那么容易。我冒昧地下载了Prototype源代码,并且无法找到它的说明文件上传对象的使用FileList数据。



事实上,我发现在线论文对序列化文件上传对象本身的问题进行了编目,声称没有AJAX库做得很好(注意,这是在2007年编写的)。这个主题很有趣,看起来你可以处理任意数量的方法来从文件上传中提取数据 - 规范本身包含足够的信息来引导你沿着这条路径前进。



我希望我有一个关于如何编写的答案,并将文件添加到文件列表中,但目前我和你一样迷失方向(或者在提问时这个问题)。多读一点,这可能比我怀疑的要容易得多 - 但目前我没有时间去投资。



祝您好运。

相关文件:




  1. W3C上的FileUpload

  2. 表格序列化作者Garrett Smith,2007
  3. Prototype 1.6.1RC3 Source (第3967行:Form.serializeElements开始)


Previously using Prototype I could serialize the input type file, it gave me the file name of the file being uploaded but when I serialized the form in jquery I just got the input type text and others not the file how do I do it?

解决方案

After spending a few minutes in Firebug, there's actually several ways to go about this it seems. For instance, I was able to get a list of files from the fileObject itself:

var file = $("#control").attr("files")[0];
var fileName = file.fileName;
var fileSize = file.fileSize;

alert("Uploading: "+fileName+" @ "+fileSize+"bytes");

Clearly I can read the values for serializing. But writing is another issue.

But apparently this isn't as easy as others claim. I took the liberty of downloading the Prototype source and couldn't find out where it's instructions were for using FileList data for the File Upload object.

In fact, I found a paper online that cataloged the issues with serializing the File Upload object itself, claiming that no AJAX Library did it well (note, this was written in 2007). This topic is interesting though, and it appears that you may be able to work up any number of methods to pull the data from the File Upload - the spec itself contains ample information to guide you down that path.

I wish I had an answer as to how you can write, and add files to the File List, but at the moment I'm just as lost as you are (or were at the time of asking this question). With a bit more reading, this may prove to be much easier than I suspect - but at the moment I don't have the time to invest.

Best of luck.

Relevant Documentation:

  1. FileUpload on W3C
  2. Form Serialization by Garrett Smith, 2007
  3. Prototype 1.6.1RC3 Source (Line 3967: Form.serializeElements begins)

这篇关于我如何序列化jquery中的文件类型输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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