hakuna
hakuna

Reputation: 6701

Download File from AutoDesk Vault (Vault 2015 SDK) with c# code giving error

I am trying to download a file from Vault (Vault 2015 SDK) using C# code. Tried the exact same approach as mentioned here: http://inventorhub.autodesk.com/discussions/threads/301/post/5600165 but getting the error

The request failed with HTTP status 404: Not Found" while executing the respective line of code for downloading the file.

Please find below my sample code:

using Autodesk.Connectivity.WebServicesTools;
using Autodesk.Connectivity.WebServices; 

UserPasswordCredentials login = new UserPasswordCredentials("servername", "myVault", "username", "Password", true);
using (WebServiceManager serviceManager = new WebServiceManager(login))
{
    Autodesk.Connectivity.WebServices.Folder folder = serviceManager.DocumentService.GetFolderByPath("$/Myfolder");
    Autodesk.Connectivity.WebServices.File[] files = serviceManager.DocumentService.GetLatestFilesByFolderId(folder.Id, false);
    if (files != null && files.Any())
    {
        foreach (Autodesk.Connectivity.WebServices.File file in files)
        {
            //Sample code to download the files
            string localPath = AppDomain.CurrentDomain.BaseDirectory;
            Autodesk.Connectivity.WebServices.File localFile = serviceManager.DocumentService.GetFileById(file.Id);
            var FileDownloadTicket = serviceManager.DocumentService.GetDownloadTicketsByFileIds(new long[] { file.Id });
            FilestoreService fileStoreService = new FilestoreService();
            var fileBytes = fileStoreService.DownloadFilePart(FileDownloadTicket[0].Bytes, 0, localFile.FileSize, false);
            System.IO.File.WriteAllBytes(localPath, fileBytes);
        }
    }
}

Getting the error at fileStoreService.DownloadFilePart(FileDownloadTicket[0].Bytes, 0, localFile.FileSize, false);. I am able to download the file manually, but not programmatically. What am I doing wrong ? Also it would be great if I could get some sample code to download a file based on the metadata.

Thanks!

Upvotes: 1

Views: 3503

Answers (2)

hakuna
hakuna

Reputation: 6701

I modified

FilestoreService fileStoreService = new FilestoreService()

to

FilestoreService fileStoreService = serviceManager.FilestoreService

in the code snipped posted in the question and it worked.

Upvotes: 1

Jason Faulkner
Jason Faulkner

Reputation: 6558

For downloading files you want to "Acquire" them.

See the SDK documention for the object: Autodesk.DataManagement.Client.Framework.Vault.Currency.Connections.Connection

Once you have created a connection object, use it to acquire the files (note this is also how you check-out files):

using VDF = Autodesk.DataManagement.Client.Framework;

var acquireSettings = new VDF.Vault.Settings.AcquireFilesSettings(
    connection, updateFileReferences: false);

foreach (var file in files)
{
    acquireSettings.AddFileToAcquire(
        new VDF.Vault.Currency.Entities.FileIteration(connection, file),
        VDF.Vault.Settings.AcquireFilesSettings.AcquisitionOption.Download);
}

VDF.Vault.Results.AcquireFilesResults results = connection.FileManager.AcquireFiles(acquireSettings);

Upvotes: 5

Related Questions