This topic is locked

Create Subfolder For Uploaded Docs

5/8/2018 2:47:37 PM
ASPRunner.NET General questions
I
i.NoLim author

Hello, I currently have a web app that takes basic information from a user and allows for multiple uploads. I would like to create a subfolder in the "Upload folder" and store the files for said session within the folder. I found this, but it's for asprunnerpro. https://xlinesoft.com/asprunnerpro/docs62/upload_files_to_usersfolders.htm
Additionally, I would like to rename the files. I created a thread here but it was locked. I still have the same errors as in the last post. http://asprunner.com/forums/topic/25119-rename-uploaded-files/pagep83615fromsearch1&#entry83615

admin 5/9/2018

You can use .NET expression to specify path to upload folder and each user files can be stored in a separate folder:

https://xlinesoft.com/asprunnernet/docs/edit_as_settings_file_image.htm
And this article explains how to deal with renaming uploaded files:

https://xlinesoft.com/asprunnernet/docs/rename_uploaded_files.htm

I
i.NoLim author 5/9/2018

I tried the following, as shown in the page below, but got an error message. "CUSTOMER_NAME" is a field.

folder = XSession.Session["CUSTOMER_NAME"]


https://xlinesoft.com/asprunnernet/docs/edit_as_settings_file_image.htm

admin 5/9/2018

This won't work. It is not enough to have a field named "CUSTOMER_NAME". You need to populate session variable XSession.Session["CUSTOMER_NAME"] manually and it needs to be done before page is loaded in event like BeforeProcess or ProcessRecordValues on Edit page.

I
i.NoLim author 5/9/2018



This won't work. It is not enough to have a field named "CUSTOMER_NAME". You need to populate session variable XSession.Session["CUSTOMER_NAME"] manually and it needs to be done before page is loaded in event like BeforeProcess or ProcessRecordValues on Edit page.



You mentioned it must be done in the Edit Page, does that mean that you cant create new folders when uploading files for the first time (Add Page)?
As for the renaming of the files, that's the code that I've been using but when the "Upload Folder" is changed to anything other than "files/" I get the error shown on my last post here. http://asprunner.com/forums/topic/25119-rename-uploaded-files/pagep83615fromsearch1&#entry83615

I
i.NoLim author 5/10/2018



This won't work. It is not enough to have a field named "CUSTOMER_NAME". You need to populate session variable XSession.Session["CUSTOMER_NAME"] manually and it needs to be done before page is loaded in event like BeforeProcess or ProcessRecordValues on Edit page.



So I thought about this and why it doesn't work; The values for the fields I'm trying to use do not exists yet when uploading a new file. I do, however, have access to the user's name because they had to log in. My "Upload folder" field now looks like this:

folder = "files/" + XSession.Session["TempVal"].ToString();


Where "TempVal" was declared in "Process record Values." I don't think I even needed to do this.

XSession.Session["TempVal"] = values["REQUESTOR_NAME"];


This works great. The only issue is that I would like to save the files in a different location but I get the error shown below.

folder = "C:\\Users\\cagarcia\\Documents\\Uploads/" + XSession.Session["TempVal"].ToString();



I would still like to use one of the field values as the name of the folder being created, is there a way to grab the values as they are being typed and assign it to a "TempVal?"

admin 5/17/2018

Files are uploaded to the server before user clicks 'Saved' button. This is why you need to specify upload folder before and data is entered.
If you need to move files to a folder named after one fields on the form you need to use events like AfterAdd and AfterEdit and move uploaded files to a new location creating this new folder if required.

I
i.NoLim author 5/21/2018



Files are uploaded to the server before user clicks 'Saved' button. This is why you need to specify upload folder before and data is entered.
If you need to move files to a folder named after one fields on the form you need to use events like AfterAdd and AfterEdit and move uploaded files to a new location creating this new folder if required.



Thank you for you reply. Yeah I realized, after some testing, that the files were being uploaded before clicking "Save."
I've been working on a "After record added" event but haven't had a lot of success. I would like to move the files to a specific location, create a subfolder based on a field name like 'CONTACT_PERSON,' and rename the file based on other fields in the form. This is what I have so far,



// get information about uploaded files

var fileArray = MVCFunctions.my_json_decode(values["UPLOAD_FILE"]);
// rename files and move them

for(int i = 0; i < fileArray.Count(); i++)

{

var fileName = fileArray[i]["name"];

var ext = System.IO.Path.GetExtension(fileArray[i]["name"]);

var newFileName = String.Format("C:\\Users\\cagarcia\\Documents\\Uploads\\{0}\\{1}" + "_" + "{2}{3}", values["CONTACT_PERSON"], values["UNIQUE_ID"], i, ext);
System.IO.File.Move(MVCFunctions.getabspath(fileName), MVCFunctions.getabspath(newFileName));

fileArray[i]["name"] = newFileName;

}
// update values of the field that stores file names

values["UPLOAD_FILE"] = MVCFunctions.my_json_encode(fileArray);