This topic is locked

Rename Uploaded Files

10/31/2017 1:11:35 PM
ASPRunner.NET General questions
I
i.NoLim author

I currently have a page where users can upload multiple files with different extensions at once. I would like to use the following naming convention "Unique_ID" + "Name" + "_XX" + extension; Where "Unique_ID" and "Name" are fields in the form, "_XX" is the number of the file, and extension is well... the extension.
For example: John Smith opens a new new form and its automatically assigned Unique_ID number 857, he then proceeds to upload files ashdkhsa.pdf, 546asw.doc, and jshh232h_64.jpg. The files should be renamed 857_JohnSmith_01.pdf, 857_JohnSmith_02.doc, and 857_JohnSmith_03.jpg respectively.
ashdkhsa.pdf = 857_JohnSmith_01.pdf

546asw.doc = 857_JohnSmith_02.doc

jshh232h_64.jpg = 857_JohnSmith_03.jpg
I already looked at this page but I'm not sure how to obtain the extension for each individual file, or how to add the file number.
Thank you!

admin 10/31/2017

You can see the code that shows how to rename files at https://xlinesoft.com/asprunnerpro/docs/rename_uploaded_files.htm To find a file extension you need to parse the file name, find the last dot and everything after that dot will be your extension.
The only flaw here is that this code normally to be placed to BeforeAdd/BeforeEdit events where your Unique ID number is not generated yet. Typically autoincrement number are generated by the database when record is added and not before that.

I
i.NoLim author 10/31/2017



You can see the code that shows how to rename files at https://xlinesoft.com/asprunnerpro/docs/rename_uploaded_files.htm. To find a file extension you need to parse the file name, find the last dot and everything after that dot will be your extension.
The only flaw here is that this code normally to be placed to BeforeAdd/BeforeEdit events where your Unique ID number is not generated yet. Typically autoincrement number are generated by the database when record is added and not before that.


I cant connect to that page, I get a 404 error. But I assume is the same page I meant to link on my original post but forgot. https://xlinesoft.com/asprunnerpro/docs62/rename_uploaded_files.htm



I already looked at this page


I tried using that code but I get the following error.

error CS0131: The left-hand side of an assignment must be a variable, property or indexer


And this code could not be added in the "After record added" event?
Edit: I managed to open the link using a different browser, will post when I have updates.

admin 10/31/2017

If you getting an error message that means you doing something wrong. This code is tested and works.
It is also possible to implement a similar technique in events like AfterAdd and AfterEdit. Instead of assigning a value to values("fieldname") you will need to build and execute a SQL query that updates "fieldname" field in the database.

I
i.NoLim author 10/31/2017

I've never actually used VB so I'm having some issues with the code. I understand its grabbing the names of the files uploaded and storing them in an array. It then uses a for loop to go into the array, grabs each individual file and renames it. I'm not entirely sure what's going on in lines 31 and 32, it seems like its moving the file? Line 38 updates the value in the DB.
https://imgur.com/vWgGILc
https://imgur.com/a/jBPM6

I
i.NoLim author 10/31/2017



If you getting an error message that means you doing something wrong. This code is tested and works.
It is also possible to implement a similar technique in events like AfterAdd and AfterEdit. Instead of assigning a value to values("fieldname") you will need to build and execute a SQL query that updates "fieldname" field in the database.



That makes sense but will this change the name of the actual file or just the name that is displayed, as mentioned in the link you provided.

admin 11/1/2017

It looks like you use ASPRunner.NET but post your questions to ASPRunnerPro forum and get ASPRunnerPro specific answers.

I
i.NoLim author 11/1/2017



It looks like you use ASPRunner.NET but post your questions to ASPRunnerPro forum and get ASPRunnerPro specific answers.



My apologies, is there a way I can get this thread moved? Or is it easier for me to close it and reopen it in the ASPRunner.NET forums?
I'm attempting to write the SQL query but its proving to be more difficult than originally thought, specially for entries with multiple upload files.

I
i.NoLim author 11/2/2017

I'm more versed in C++ so my pseudo code is a mixture of it and SQL, now I just need to "translate" it,



int j = 1
int x = count "{" in "UPLOAD_FILE"
for(int i = 1; i <= x; i++)

{

substring_index(substring_index(UPLOAD_FILE, "{", i), "}", i) as part_j;

replace([UPLOAD_FILE, substring_index(substring_index(part_j, "_", 1), "/", -1), [UNIQUE_ID])
j++;

}


This is what my UPLOAD_FILE field looks like:

[{"name":"C:\\Users\\username\\Documents\\Uploads/abcd_udod4v5p.jpg","usrName":"abcd.jpg","size":1013329,"type":"image/jpeg","searchStr":"abcd.jpg,!12345.txt,!:sStrEnd"},{"name":"C:\\Users\\username\\Documents\\Uploads/12345_o6sq520f.txt","usrName":"12345.txt","size":288,"type":"text/plain"}]
I
i.NoLim author 11/8/2017

I ended up using the following code in the "After record added" event. My only issue is when the upload folder is changed to something like C:\Users\nameofuser\Documents\Uploads. I've attached pictures of my code, error, whats displayed in my DB field, and the settings for said field. As you can see from the third picture, the path in the DB has 2 backslashes; this is most likely causing the issue.
https://www.xlinesoft.com/asprunnernet/docs/rename_uploaded_files.htm