Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to rename the file before upload? #138

Closed
alanapradhan opened this issue May 23, 2014 · 13 comments
Closed

Is it possible to rename the file before upload? #138

alanapradhan opened this issue May 23, 2014 · 13 comments
Labels

Comments

@alanapradhan
Copy link

No description provided.

@nervgh
Copy link
Owner

nervgh commented May 25, 2014

Please, see third item of faq

@davidwparker
Copy link
Contributor

@nervgh - looking at the faq, which leads to this comment #97 (comment)

I'm a little confused. From what I can see, there's no actual way to edit item.file attributes.

Just so I'm clear, what you're suggesting is to edit item.formData and set whatever we want the name attribute to be, and then re-naming it on the backend, correct?

@davidwparker
Copy link
Contributor

Figured it out doing exactly that (setting formData and renaming on the backend).

That said, it would be nice to know that you can't just change the item.file values- I'm sure this is due to some security API stuff with browsers though...

@alanapradhan
Copy link
Author

@davidwparker I am directly uploading to amazon S3 after receiving a signed signature from my nodejs server. I ultimately decided to auto increment the directory name and keep the original file name in-tact. ie uploads/12/businessModel.doc, uploads/13/businessModel.doc. I think this is how asana decided to to prevent naming collisions.

@nervgh
Copy link
Owner

nervgh commented Jun 18, 2014

Just so I'm clear, what you're suggesting is to edit item.formData and set whatever we want the name attribute to be, and then re-naming it on the backend, correct?

You can use item.alias property. See docs

@nervgh nervgh closed this as completed Jun 23, 2014
@hellsan631
Copy link

I want to just leave a solution here, because @nervgh 's comments didn't help for me, item.alias didn't work, and the docs/faq didn't help.

The solution is to change item.file.name, here is a small function that works well:

uploader.onAfterAddingFile = function(item) {
  var fileExtension = '.' + item.file.name.split('.').pop();

  item.file.name = Math.random().toString(36).substring(7) + new Date().getTime() + fileExtension;
};

When you process the upload of the file, it should get renamed correctly. there is an item._file that has all of the original attributes that is used during the upload.

@AradhanaR
Copy link

Thanks @hellsan631 ... it worked for me!

@materkel
Copy link

materkel commented Oct 7, 2015

Thanks @hellsan631 👍

@israelsaraiva
Copy link

Not works on IE9.

@askdesigners
Copy link

There's a nastier problem here though in that you can have users uploading files with character that will break AWS links. Files like !!měl být ekono()mic+=ký růst tažen^%&$$ 2.png will not work at all. If it seems like a corner case, then you haven't dealt with real user files much. :) People do weird shit.

@tatsujb
Copy link

tatsujb commented Sep 25, 2019

yes!

I am in this case :

  /*
   * type false === image, true === document
   */
  addedFile(e, type) {
    const formData: FormData = new FormData();
    const File = e.addedFiles[0];
    const oldName = File.name;
    const fileExtension = oldName.slice(oldName.lastIndexOf('.') - oldName.length);
    const str = oldName.slice(0, oldName.lastIndexOf('.')).replace(/[^\w]/gi, '');
    Object.defineProperty(File, 'name', {
      writable: true,
      value: str + fileExtension
    });
    formData.append('files[]', File);
    if(type){
      this.api.uploadDocument(formData).subscribe();
    } else {
      this.api.uploadImage(formData).subscribe();
    }
  }

this fails (api call level, on answer, it is pending until timeout) because the variable File is just a pointer of some sort to actual computer file access and renaming it's name means that the File won't be found on the disk (without JS or angular errors, without any errors at all).

This is pretty stupid.

I'm actually fine with renaming the user's file on disk before upload. the user is stupid and needs to have his hand held.

@fredyfx
Copy link

fredyfx commented Jan 28, 2020

Thank you very much! @hellsan631

@Merlincool
Copy link

Okay I am struggling to understand this.

it would be glad if someone helps me here.

my file name is My File @12345.txt
this is file I want to upload
but I want this filename to be changed to My File.txt before it's uploaded, so server don't get that numerical values along with special character. Is that possible anyways?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests