1


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.

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. For example, "file.zip" to "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. 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. 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.

<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>
Community
  • 1
  • 1
nqw1
  • 979
  • 3
  • 11
  • 16

1 Answers1

0

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. 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.

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

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • I don't think he was asking about actually zipping the files prior to upload. The assumption is that there's foo.zip, and he believes by changing it to foo.zip.jpg that the server will allow the upload. – Visionary Software Solutions Aug 17 '11 at 13:09
  • I said nothing about zipping. The original file name is part of the upload as well. – Álvaro González Aug 17 '11 at 14:15
  • Thank you for the clarification! I understood that all the modern web browsers support it except IE and that's fine for me. I try to avoid using Flash so this seems to be the best option. I still think about doing it on the server-side but I'm concerned how it'll affect on writing metadata for the files. I assume that first I'd have to upload the file, then change file extension and then write metadata and then move it to Elvis. However, I was unable write any working code to do this so I still need help with the code to do it on the client-side. – nqw1 Aug 18 '11 at 06:29
  • @nqw1 - I don't know what your server-side technology is but in (e.g.) Apache+PHP uploaded files are saved with a random unique name in a temporary directory; it's up to you whether to use the original name for anything. Sorry I can't help you with the W3C File API: I've never used it. – Álvaro González Aug 18 '11 at 06:55