How to upload and check in a modified file to vault

How to upload and check in a modified file to vault

pzuziak
Contributor Contributor
3,389 Views
11 Replies
Message 1 of 12

How to upload and check in a modified file to vault

pzuziak
Contributor
Contributor

Hello,

How are you doing guys?

I was wondering if someone could point me in the right direction as I spent a few fruitless hours trying to solve my problem.

I download and check out a pdf to the local work space where I modify it next I am trying to check it back into the vault but without success. I tried: 

 

docServ.CheckinUploadedFile(pdf.MasterId, pdfComment, false, DateTime.Now, pdfAssocParamArray, null, false, pdf.Name, pdf.FileClass, false, null);

 

which just rolls back the file.

I also tried .....Connection.FileManager.AddFile(......) but it gives me error 1008 - file already exists.

 

Any help would be greatly appreciated.

 

Thenks

Przemek

 

0 Likes
Accepted solutions (1)
3,390 Views
11 Replies
Replies (11)
Message 2 of 12

sajith_subramanian
Autodesk Support
Autodesk Support

Hi @pzuziak,

 

The CheckinUploadedFile method will roll back the file if it finds that the file has been unchanged in any way.

Probably, your code is not checking in the correct file (the one that is been modified in your local workspace).

Before making a call to CheckinUploadedFile, use the UploadFilePart API first. It will return a byte array, which needs to be passed to CheckinUploadedFile in the last argument(UploadTicket).

Here is a link to a C# code for reference:

https://justonesandzeros.typepad.com/Files/LegacyFileTransfer/LegacyFileTransfer.cs

 

Hope this helps.

 

Regards,

Sajith

 

 

 


Sajith Subramanian
Autodesk Developer Network
0 Likes
Message 3 of 12

pzuziak
Contributor
Contributor

Hi @sajith_subramanian,

Thank you for you reply - it got me closer to the solution but not quite there yet...

I guess there were some changes in the Vault API since the code examples you linked were published as you cannot use byte array any more - you have to use stream instead.

Here is how I am calling my method:

 

                                    Stream fileCont = new FileStream(pdfPath, FileMode.Open,FileAccess.Read);
                                    ByteArray uPloadTicket = UploadFile(connection.WebServiceManager, "TRAINING", pdf.Name, fileCont);
                                    

And this is the method:

 

 

        private static ByteArray uploadFile(ACWT.WebServiceManager mgr, string vaultName, string fileName, Stream fileContents)        {

            mgr.FilestoreService.CompressionHeaderValue = new CompressionHeader();
            mgr.FilestoreService.CompressionHeaderValue.Supported = Compression.None;
            FileTransferHeader fileTransferHeaderValue = new FileTransferHeader();
            mgr.FilestoreService.FileTransferHeaderValue = fileTransferHeaderValue;
            mgr.FilestoreService.FileTransferHeaderValue = null;

            fileTransferHeaderValue.Identity = Guid.NewGuid();
            fileTransferHeaderValue.Extension = System.IO.Path.GetExtension(fileName);
            fileTransferHeaderValue.Vault = vaultName;
            fileTransferHeaderValue.Compression = Compression.None;
            
            ByteArray uploadTicket = new ByteArray();
            uploadTicket.Bytes = mgr.FilestoreService.UploadFilePart(fileContents);        
            return uploadTicket;
        }

When the code gets to the UploadFilePart  I get an error "155" for which I cannot find any meaningful description on the internet.

I guess I am doing something wrong when setting up the stream or setting up the header....

 

Kind regards

Przemek

 

 

0 Likes
Message 4 of 12

sajith_subramanian
Autodesk Support
Autodesk Support
Accepted solution

Hi @pzuziak,

 

Could you try the below code, and check if it works for you.

 

        public static ByteArray UploadFileResource(WebServiceManager svcmgr, string filename, byte[] fileContents)
        {
            svcmgr.FilestoreService.FileTransferHeaderValue = new FileTransferHeader();
            svcmgr.FilestoreService.FileTransferHeaderValue.Identity = Guid.NewGuid();
            svcmgr.FilestoreService.FileTransferHeaderValue.Extension = Path.GetExtension(filename);
            svcmgr.FilestoreService.FileTransferHeaderValue.Vault = svcmgr.WebServiceCredentials.VaultName;

            ByteArray uploadTicket = new ByteArray();
            int bytesTotal = (fileContents != null ? fileContents.Length : 0);
            int bytesTransferred = 0;
            do
            {
                int bufferSize = (bytesTotal - bytesTransferred) % MAX_FILE_TRANSFER_SIZE; 
                byte[] buffer = null;
                if (bufferSize == bytesTotal)
                {
                    buffer = fileContents;
                }
                else
                {
                    buffer = new byte[bufferSize];
                    Array.Copy(fileContents, (long)bytesTransferred, buffer, 0, (long)bufferSize);
                }

                svcmgr.FilestoreService.FileTransferHeaderValue.Compression = Compression.None;
                svcmgr.FilestoreService.FileTransferHeaderValue.IsComplete = (bytesTransferred + bufferSize) == bytesTotal ? true : false;
                svcmgr.FilestoreService.FileTransferHeaderValue.UncompressedSize = bufferSize;

                using (var fileContentsStream = new MemoryStream(fileContents))
                    uploadTicket.Bytes = svcmgr.FilestoreService.UploadFilePart(fileContentsStream);
                bytesTransferred += bufferSize;

            } while (bytesTransferred < bytesTotal);

            return uploadTicket;
        }

Regards,

Sajith


Sajith Subramanian
Autodesk Developer Network
Message 5 of 12

pzuziak
Contributor
Contributor

Hi @sajith_subramanian,

 

It worked a treat, Thanks a lot!!!

 

Kind regards

Przemek

0 Likes
Message 6 of 12

ThomasRambach
Advisor
Advisor

@sajith_subramanian How do you call that method?

0 Likes
Message 7 of 12

sajith_subramanian
Autodesk Support
Autodesk Support

@ThomasRambach : If I understand correctly - Do you mean UploadFileResource method?  

You pass in as arguments, The WebserviceManager, the name of the file and its contents as a Byte array. It will return an UploadTicket, which can be used as an argument to CheckinUploadedFile method.

 

Regards,

Sajith

 

 


Sajith Subramanian
Autodesk Developer Network
Message 8 of 12

ThomasRambach
Advisor
Advisor

@sajith_subramaniangot it. I found some example code that works for what I need.

0 Likes
Message 9 of 12

SMOHANDOSS
Explorer
Explorer
Hi mate, I am trying to check-in a file into vault from my local, for that I need upload ticket, using this code in Powershell and getting a "155" exception. can anyone help me to solve this.
$fileContents = [System.IO.File]::ReadAllBytes($localPath)
$uploadTicket = Upload-FileResource -vault $vault -filename $fileName -fileContents $fileContents
function Upload-FileResource{
    param (
        [Object] $vault,
        [string] $filename,
        [byte[]] $fileContents
    )
    $headerObject = New-Object Autodesk.Connectivity.WebServices.FileTransferHeader
    $headerObject.Identity = New-Guid
    $headerObject.Extension = [System.IO.Path]::GetExtension($filename)
    $headerObject.Vault = $vault.WebServiceCredentials.VaultName
    $uploadTicket = New-Object Autodesk.Connectivity.WebServices.ByteArray
    $bytesTotal = if ($null -ne $fileContents) { $fileContents.Length } else { 0 }
    $bytesTransferred = 0
    do {
        $bufferSize = $bytesTotal - $bytesTransferred
        if ($bufferSize -gt (49 * 1024 * 1024)) {
            $bufferSize = (49 * 1024 * 1024)
        }
 
        $buffer = $fileContents[$bytesTransferred..($bytesTransferred + $bufferSize - 1)]
        $headerObject.Compression = "None" 
        $headerObject.IsComplete = ($bytesTransferred + $bufferSize) -eq $bytesTotal
        $headerObject.UncompressedSize = $bufferSize
$fileContentsStream = [System.IO.MemoryStream]::new($buffer)
        $uploadTicket.Bytes = $vault.FilestoreService.UploadFilePart($fileContentsStream)
        $bytesTransferred += $bufferSize
    } 
    while ($bytesTransferred -lt $bytesTotal)
    return $uploadTicket
    }
 
Exception calling "UploadFilePart" with "1" argument(s): "155"
@pzuziak @ThomasRambach @sajith_subramanian 

 

0 Likes
Message 10 of 12

jason.dupree
Advocate
Advocate

Hi, I'm working through doing this in Powershell. You mention when using the UploadFileResource method, 

you pass in as arguments, the name of the file and its contents as a Byte array. 

How do you get Byte array?

(I'll hopefully figure this out before you respond, but I hoping it'll help anyone else that finds this thread with the same question.)

Jason Dupree
Senior Consultant - ECAD, Inc.
0 Likes
Message 11 of 12

jason.dupree
Advocate
Advocate

I'm stuck at the same point as the previous message. I've converted the byte array to a Memory Stream and am getting the 155 error. This is in Powershell using Cool Orange's PowerVault.

    $fileBytes = [System.IO.File]::ReadAllBytes($FileVersion.FromPath)
    $fileBytes = New-Object -TypeName 'System.IO.MemoryStream' -ArgumentList (,$fileBytes)
    $Uploadticket = $Vault.FilestoreService.uploadfilepart($fileBytes)
  
Jason Dupree
Senior Consultant - ECAD, Inc.
0 Likes
Message 12 of 12

ThomasRambach
Advisor
Advisor

@jason.dupree This is my C# code for File Uploads, it may be helpful to reference:

        public static ACW.ByteArray UploadFileResource(WebServiceManager svcmgr, string filename, byte[] fileContents)
        {
            svcmgr.FilestoreService.FileTransferHeaderValue = new ACW.FileTransferHeader();
            svcmgr.FilestoreService.FileTransferHeaderValue.Identity = Guid.NewGuid();
            svcmgr.FilestoreService.FileTransferHeaderValue.Extension = System.IO.Path.GetExtension(filename);
            svcmgr.FilestoreService.FileTransferHeaderValue.Vault = svcmgr.WebServiceCredentials.VaultName;

            ACW.ByteArray uploadTicket = new ACW.ByteArray();

                
                int bytesTotal = (fileContents != null ? fileContents.Length : 0);
                int bytesTransferred = 0;
                do
                {
                    int bufferSize = (bytesTotal - bytesTransferred); //% MAX_FILE_TRANSFER_SIZE;
                    byte[] buffer = null;
                    if (bufferSize == bytesTotal)
                    {
                        buffer = fileContents;

                    }
                    else
                    {
                        buffer = new byte[bufferSize];
                        Array.Copy(fileContents, (long)bytesTransferred, buffer, 0, (long)bufferSize);
                    }

                    svcmgr.FilestoreService.FileTransferHeaderValue.Compression = ACW.Compression.None;
                    svcmgr.FilestoreService.FileTransferHeaderValue.IsComplete = (bytesTransferred + bufferSize) == bytesTotal ? true : false;
                    svcmgr.FilestoreService.FileTransferHeaderValue.UncompressedSize = bufferSize;

                    using (var fileContentsStream = new MemoryStream(fileContents))
                        uploadTicket.Bytes = svcmgr.FilestoreService.UploadFilePart(fileContentsStream);
                    bytesTransferred += bufferSize;

                } while (bytesTransferred < bytesTotal);

                return uploadTicket;


            
        }
0 Likes