简体   繁体   English

使用 Javascript 添加新文件扩展名而不删除旧文件扩展名

[英]Add new file extension with Javascript without removing old one


There has been few similar questions ( here and here ) but not exactly what I want and I haven't been able to combine the codes.几乎没有类似的问题( herehere ),但不完全是我想要的,我无法组合代码。

I have an upload form and if user uploads ZIP or RAR file, I want to add.jpg file extension without removing.zip or.rar extension.我有一个上传表单,如果用户上传 ZIP 或 RAR 文件,我想添加.jpg 文件扩展名而不删除.zip 或.rar 扩展名。 For example, "file.zip" to "file.zip.jpg".例如,“file.zip”到“file.zip.jpg”。

I need this because users can upload images and PDF files into my server but if somebody has multiple files, it's better to zip them.我需要这个,因为用户可以将图像和 PDF 文件上传到我的服务器,但如果有人有多个文件,最好是 zip 他们。 I also write certain metadata for the uploaded files based on the selections user has made in the upload form.我还根据用户在上传表单中所做的选择为上传的文件编写某些元数据。 Metadata is for the Elvis so I can move the pictures around my server based on the metadata.元数据是给猫王的,所以我可以根据元数据在我的服务器上移动图片。 Problem is that certain metadata can't be written in ZIP files in Elvis at the moment so my workaround is to change the file extension as.jpg to fool Elvis and write metadata for the file.问题是某些元数据目前无法写入猫王的 ZIP 文件中,因此我的解决方法是将文件扩展名更改为.jpg 以欺骗猫王并为该文件写入元数据。 Yes, this is a bad workaround but at the moment it serves my purposes.是的,这是一个糟糕的解决方法,但目前它符合我的目的。

I don't have much code ready because I don't know where should I begin.我没有准备好太多代码,因为我不知道应该从哪里开始。 Also, you won't drop from the chair when you see my JS functions;) JS code can be written with jQuery.另外,当你看到我的 JS 函数时,你不会从椅子上掉下来;) JS 代码可以用 jQuery 编写。

<form action="http://url.here" method="post" enctype="multipart/form-data" id="form" onsubmit="addExtension();">
  <label for="file">File </label>
  <input type="file" name="Filedata" id="file">
  <input type="submit" value="Upload" />
</form>

The questions you link basically ask about string manipulation.您链接的问题基本上是关于字符串操作的。 However, you want to alter a file upload programmatically, which is an entirely different subject.但是,您想以编程方式更改文件上传,这是一个完全不同的主题。 Historically, most browsers explicitly disallow it for security reasons.从历史上看,大多数浏览器出于安全原因明确禁止它。 Some modern browsers implement the W3C File API ( here's an example for Firefox ) which gives kind of full control about file uploads.一些现代浏览器实现了 W3C文件 API这里是 Firefox 的示例),它可以完全控制文件上传。 I don't know how widespread its support is (probably, it doesn't work on Internet Explorer) but that's your only chance to do it client-side unless you are willing to switch to Flash.我不知道它的支持有多广泛(可能,它在 Internet Explorer 上不起作用)但这是您在客户端执行此操作的唯一机会,除非您愿意切换到 Flash。

IMHO, it looks more sensible to do it server-side but I'm not sure about how much freedom you have (it looks that Elvis is some sort of black box).恕我直言,在服务器端做这件事看起来更明智,但我不确定你有多少自由(看起来猫王是某种黑匣子)。

Edit: I just found this: http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(HTML5)#Related_specifications编辑:我刚刚发现: http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(HTML5)#Related_specifications

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM