Upload file to Gallery REST SDK

Trying to use the REST SDK to upload a file to a gallery.  Everything looks like it works, but the file seems corrupted when I try download/view it from my community.  I'm not entirely how to pass in the filedata as a byte array as the method asks for a string and not sure I'm encoding it correctly.

byte[] fileData = System.IO.File.ReadAllBytes(myPdfPath);

                    // Add as a new media item to Telligent
                    var response = host.PostToDynamic(2, "media/{mediagalleryid}/files.json", false, new RestPostOptions
                    {
                        PathParameters = new System.Collections.Specialized.NameValueCollection {
                            { "mediagalleryid", "246" }
                        },
                        PostParameters = new System.Collections.Specialized.NameValueCollection {
                            { "Name", resourceFileName },
                            { "FileName", resourceFileName},
                            { "FileData", String.Join(" ", fileData)},
                            { "ContentType", MimeMapping.GetMimeMapping(resourceFileName)},
                            { "Tags", tags},
                            { "Description", description}
                        }
                    });



Updated
[edited by: Luke R Davidson at 11:25 AM (GMT 0) on Fri, Dec 18 2020]
Parents Reply
  • Former Member
    0 Former Member in reply to Patrick M.

    Hi guys! Have you found a solution to the "Access to upload files is denied." error message? I am getting the same error message when trying to upload temp files. Here's my code:

        class Program
        {
            static void Main(string[] args)
            {
                const string USER_NAME = "admin";
                const string OAUTH_CLIENT_ID = "";
                const string OAUTH_SECRET = "";
                var host = new ClientCredentialsRestHost(USER_NAME, "http://localhost/", OAUTH_CLIENT_ID, OAUTH_SECRET);
        
                string[] files = Directory.GetFiles("C:\\PSP", "*.jpg");
    
                foreach (string f in files)
                {
    
                    using (var fileStream = new FileStream(f, FileMode.Open, FileAccess.Read))
                    {
                        fileStream.Position = 0;
                        Guid uploadContextId = Guid.NewGuid();
                        UploadedFile file = new UploadedFile(uploadContextId, f, "image/jpg", fileStream);
    
                        UploadedFileInfo fileInfo = host.UploadFile(file, new RestFileOptions()
                        {
                            UploadProgress = (p) =>
                            {
                                Console.WriteLine("Percent Complete:" + p.PercentComplete);
                            }
                        });
    
                        //if (!fileInfo.IsError)
                        //{
                        //    Create
                        //}
                    }
                }
            }
        }

    I also tried a different approach, but I'm getting a different error message:

        class Program
        {
            static void Main(string[] args)
            {
                const string USER_NAME = "admin";
                const string OAUTH_CLIENT_ID = "";
                const string OAUTH_SECRET = "";
                var host = new ClientCredentialsRestHost(USER_NAME, "http://localhost/", OAUTH_CLIENT_ID, OAUTH_SECRET);
        
                string[] files = Directory.GetFiles("C:\\PSP", "*.jpg");
    
                foreach (string f in files)
                {
    
                        Guid uploadContextId = Guid.NewGuid();
                        var response = host.PostToDynamic(2, "cfs/temporary.json", false, new RestPostOptions
                        {
                            PostParameters = new NameValueCollection {
                            { "UploadContextId",  uploadContextId.ToString() },
                            { "FileName", f }
                            }
                        });
    
                    Console.WriteLine(response.Errors[0]);
    
                }
            }
        }

    With the above I'm getting:

    "Errors, no file was found in the request or the file could not be read.".

Children
No Data