# How to upload multiple files.

I want to upload multiple files. I created the following method, but it does not work well. Where is it wrong? ************************************************************ //Aras Innovator 11.0 SP12 //Server side , c# Method Item fileObj1 = inn.newItem("File", "add"); fileObj1.setProperty("filename", "filename1.txt"); fileObj1.attachPhysicalFile(filepath1); fileObj1 = fileObj1.apply(); Item fileObj2 = inn.newItem("File", "add"); fileObj2.setProperty("filename", "filename2.txt"); fileObj2.attachPhysicalFile(filepath2); fileObj2 = fileObj2.apply();                       // <-- Here Innovator becomes busy. ************************************************************   Thank you.
• I also have one use case where I handle a few files at once, basically I do the same as you. Try to add more error handling, maybe this leads you to the right track:
Item fileObj1 = inn.newItem("File", "add");
fileObj1.setProperty("filename", "filename1.txt");
fileObj1.attachPhysicalFile(filepath1);
fileObj1 = fileObj1.apply();
if (fileObj1.isError())
{
return inn.newError("Error in fileObj1 " + fileObj1.getErrorDetail());
}
// Delete temporary File from Server
FileInfo fileInfo = new FileInfo(filepath1);
fileInfo.Delete();

fileObj2.setProperty("filename", "filename2.txt");
fileObj2.attachPhysicalFile(filepath2);
fileObj2 = fileObj2.apply();
if (fileObj2.isError())
{
return inn.newError("Error in fileObj2 " + fileObj2.getErrorDetail());
}   
• Thank you for your reply. As your advise, I added error handling but it still does not work. The first one is uploadable, but Innovator does not work with the second one. (Innovator freezes unless it restarts in IIS.) It seems that Innovator freezes at "fileObj2.apply();" and an error isn't returned. Is there any other wrong code? Or, please let me know if there is a sample code that has been confirmed.
• How does your filepath2 look like? Somehow like this? string path = @"C:\\temp\\text.txt"; You can take a look at this sample, but basically it does the same as your code: github.com/.../AIP_get_SSRS_Report.cs This sample also contains some code for the file upload. I use an extended version of this and sometimes trigger it 2-3 times in turn. Never had any trouble.
• Thank you for your reply. The file path is as follows. string filepath1 = @ "C:\\Aras\\filename1.txt"; string filepath2 = @ "C:\\Aras\\filename2.txt"; And these files certainly exist.
• Hi Sam, There are a few different approaches to accomplish this, but first I want to point out that you may have a problem with the definition of your file paths. Using the @ character before a string removes the need to escape slash characters. You should update your file path definitions to be like below.
string filepath1 = @“C:\Aras\filename1.txt”;
string filepath2 = @“C:\Aras\filename2.txt”;
In terms of uploading multiple files, is there a particular reason you are creating File items directly like this? Typically, I would expect these files to be related to a Document or another ItemType. If you relate these Files to an item instead, you can upload multiple files using the sample below.
Item doc = inn.newItem("Document", "add");
doc.setProperty("item_number", "My doc");
//First file
Item docFile = doc.createRelationship("Document File", "add");
docFile.setFileProperty("related_id", filepath1);
// Second file
Item docFile2 = doc.createRelationship("Document File", "add");
docFile2.setFileProperty("related_id", filepath2);
// Apply
return doc.apply();
If you need to upload these as File items without containers, I would recommend using the CheckinManager class which was built for this kind of use case. You can see an example of using this class below.
Item fileUpload = inn.newItem("tmp");
// File 1
file1.setProperty("filename", "filename1.txt");
file1.setProperty("actual_filename", filepath1);
file1.setProperty("checkedout_path", @"C:\Aras");
located.setProperty("related_id", "67BBB9204FE84A8981ED8313049BA06C"); // default vault
// File 2
file2.setProperty("filename", "filename2.txt");
file2.setProperty("actual_filename", filepath2);
file2.setProperty("checkedout_path", @"C:\Aras");
located2.setProperty("related_id", "67BBB9204FE84A8981ED8313049BA06C"); // default vault
// Apply
return cm.Checkin(1);`