This topic is locked

Rename Uploaded File by Basic Upload Control

11/13/2017 5:38:10 AM
ASPRunner.NET General questions
C
ccvvccc author

Hi all,
I am using File Upload control but Basic Upload Control is selected.
So, only filename is written to my DB.
For example; I upload test.jpg file and the DB value is test.jpg, and the file goes to my folder. There is no problem with it.
But I would like to add a prefix to my uploaded file name. Such as Id of current record. Ex: 123_test.jpg
I found the link https://www.xlinesoft.com/asprunnernet/docs/rename_uploaded_files.htm but it is for multiple file uploaded version.
I have changed it a little bit and tried the following code but no success. Anybody have an idea?
var fileName = values["Image"];
var newFileName = String.Format("{0}{1}", values["Id"], values["Image"]);
System.IO.File.Move(MVCFunctions.getabspath("images_folder/" + fileName), MVCFunctions.getabspath("images_folder/" + newFileName));
values["Image"] = newFileName;

I
i.NoLim 11/13/2017

That code should work for one or multiple files, what error do you get when you use it?

A
Arkie 11/13/2017



That code should work for one or multiple files, what error do you get when you use it?


You might consider a change in your database as opposed to changing all the file names. i.e. field 1 = username and field 2 being the uploaded files. Define field 2 as being a memo field and you can upload multiple files to that field and they will all be related to field 1 and yet keep their original file names.
Then when you view this db in a list, set the property of field 2 to be viewed as a file and select show file name. The result will be field 1 followed by a memo field of all of the uploaded file names in field 2 for the record in field 1.
field 1 image 1

image 2

image 3
All my uploads are photos (.jpg). I would think think that other file types would work the same way, but I've not yet tried it.
Hope this makes sense and hope it helps. This may well depend on what and how you use your db, but it sure works for me.

~Joe

C
ccvvccc author 11/19/2017



That code should work for one or multiple files, what error do you get when you use it?


Hi,
I got the following error.
Could not find file 'C:\Users\user\Documents\ASPRunnerNETProjects\project_name\output\0'.
by the way, I am writing my code in "After record added" event.

C
ccvvccc author 11/19/2017



You might consider a change in your database as opposed to changing all the file names. i.e. field 1 = username and field 2 being the uploaded files. Define field 2 as being a memo field and you can upload multiple files to that field and they will all be related to field 1 and yet keep their original file names.
Then when you view this db in a list, set the property of field 2 to be viewed as a file and select show file name. The result will be field 1 followed by a memo field of all of the uploaded file names in field 2 for the record in field 1.
field 1 image 1

image 2

image 3
All my uploads are photos (.jpg). I would think think that other file types would work the same way, but I've not yet tried it.
Hope this makes sense and hope it helps. This may well depend on what and how you use your db, but it sure works for me.

~Joe


Thanks for your post, but in my scenario, I just would like to rename image on "After record added" event by basic upload control. In other words; on the fly.