To be clear, you are not uploading the file twice in your current solution, right? You should only be uploading once, storing it in a temporary location, displaying it on the crop page, and then sending the crop parameters back on the second action.
Traditionally, there has been no way to access the contents of a file or the value of the file upload form. There would be a security risk letting a web page know the structure of your file system. (Are you on Windows, on an admin account, ...?) Eons ago you could do this, but we got wise.
The File API introduced in HTML5 makes it possible to access files without revealing this information, and there are some cool options available to your client-side application, the key ones being the files
property of a file input and URL.createObjectURL
.
当您更改表单时,输入中文件的内部表示会使用fileInput.files
which 是一个FileList
对象来公开。API 存在于其中您可以读取字节但您想将其设置为图像源的位置。
由于文件不是 URL,URL.createObjectURL
因此围绕文件创建一个虚拟 URL,该 URL 只能由您的页面和同源 iframe 使用。将图像设置为此,然后加载,撤销 URL 并启动您的 jQuery 裁剪插件:
input.addEventListener('change', function () {
preview.src = URL.createObjectURL(this.files[0]);
});
preview.addEventListener('load', function () {
URL.revokeObjectURL(this.src);
alert('jQuery code here. Src: ' + this.src + ', W: ' + this.width + ', H: ' + this.height);
});
至少在 Chrome 和 Firefox 中试用这个 jsFiddle 。这显然不是适用于所有浏览器的解决方案,但对于支持它的浏览器来说,它是增强它的好方法。