Unable to Upload A File to Gallery with C# REST API with Error Message

I am using the following code to attempt to upload a file programmatically using C# REST API to an existing Gallery. I have tried to Update the Gallery and also Post to the gallery and I keep getting the message: "Access to Upload Files Denied" error from fileInfo.Message. Please let me know what causes this message and how to get the code to work properly.

var driveItemPath = Path.Combine(@"C://CompanyName//", fileName);

using (var driveItemFile = System.IO.File.Create(driveItemPath))
{
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(driveItemFile);
}
//Set credentials
const string USER_NAME = "admin";
const string OAUTH_CLIENT_ID = "";
const string OAUTH_SECRET = "";


using (var fileStream = new FileStream(driveItemPath, FileMode.Open, FileAccess.Read))
{
fileStream.Position = 0;
Guid uploadContextId = Guid.NewGuid();
UploadedFile file2 = new UploadedFile(uploadContextId, fileName, MimeMapping.GetMimeMapping(fileName), fileStream);

var host = new ClientCredentialsRestHost(USER_NAME, "">http://company.com", OAUTH_CLIENT_ID, OAUTH_SECRET);
UploadedFileInfo fileInfo = host.UploadFile(file2);

try
{
host.Impersonate("admin", (h) =>
{
// Post file to Existing Gallery
var pathParameters = new NameValueCollection();
pathParameters.Add("mediagalleryid", "6");
dynamic mediaResponse = host.PostToDynamic(2, "media/{mediagalleryid}/files.json", true,
new RestPostOptions()
{
PathParameters = pathParameters,
PostParameters = new System.Collections.Specialized.NameValueCollection {
{ "Name", fileName },
{ "FileName", fileName.Trim()},
{ "FileUploadContext", fileInfo.UploadContext.ToString()},
{ "ContentType", MimeMapping.GetMimeMapping(fileName)}
}
});

Parents
  • You are trying ti impersonate the admin with admin , please try removing the impersonation  as below

    using (var fileStream = new FileStream(driveItemPath, FileMode.Open, FileAccess.Read))
    {
    fileStream.Position = 0;
    Guid uploadContextId = Guid.NewGuid();
    UploadedFile file2 = new UploadedFile(uploadContextId, fileName, MimeMapping.GetMimeMapping(fileName), fileStream);
    
    var host = new ClientCredentialsRestHost(USER_NAME, "">http://company.com", OAUTH_CLIENT_ID, OAUTH_SECRET);
    UploadedFileInfo fileInfo = host.UploadFile(file2);
    
    try
    {
    
    // Post file to Existing Gallery
    var pathParameters = new NameValueCollection();
    pathParameters.Add("mediagalleryid", "6");
    dynamic mediaResponse = host.PostToDynamic(2, "media/{mediagalleryid}/files.json", true,
    new RestPostOptions()
    {
    PathParameters = pathParameters,
    PostParameters = new System.Collections.Specialized.NameValueCollection {
    { "Name", fileName },
    { "FileName", fileName.Trim()},
    { "FileUploadContext", fileInfo.UploadContext.ToString()},
    { "ContentType", MimeMapping.GetMimeMapping(fileName)}
    }
    });
    
     }

  • So this line below, to be exact, seems to be the culprit. There is nothing in the gallery settings that I know of says that I should be getting "Access to upload files is denied" when I check it during runtime while hovering fileInfo. Does it work for you? It simply isn't uploading to my dev environment. Is there a setting that I am missing? I have uploaded a test file manually to that gallery so I know that it exists. To where does the file upload with this line of code? Maybe that directory needs additional permissions on the server level or no?

    UploadedFileInfo fileInfo = host.UploadFile(file2);

Reply
  • So this line below, to be exact, seems to be the culprit. There is nothing in the gallery settings that I know of says that I should be getting "Access to upload files is denied" when I check it during runtime while hovering fileInfo. Does it work for you? It simply isn't uploading to my dev environment. Is there a setting that I am missing? I have uploaded a test file manually to that gallery so I know that it exists. To where does the file upload with this line of code? Maybe that directory needs additional permissions on the server level or no?

    UploadedFileInfo fileInfo = host.UploadFile(file2);

Children