Skip to main content

Hi everyone


I have written PowerShell script to upload different files to needed folders in Box


API call return normal response and the files appear in box, but just txt files have content. PDF are empty and for other files just download button. After download the files are broken.


Request body contains boundary and the content of file gotten using StremReader.


For each file type I sent its own Content-Type selected by extension.


$fileName = (Get-Item $filePath).Name



            # Determine the content type based on file extension

$contentType = Get-ContentType $filePath



$line = '{"name":"' + $fileName + '", "parent":{"id":"' + $parentId + '"}}'



# Open a FileStream to read the file content

$fileStream = [System.IO.File]::OpenRead($filePath)



# Create a stream reader to read the file content

$streamReader = [System.IO.StreamReader]::new($fileStream)

$fileContent = $streamReader.ReadToEnd()



# Create the boundaries for the multipart/form-data request

$boundary = "------------------------" + [System.Guid]::NewGuid().ToString()



# Build the request body

$body = (

"--$boundary",

'Content-Disposition: form-data; name="attributes"',

'',

$line,

"--$boundary",

"Content-Disposition: form-data; name='file'; filename='$fileName'",

"Content-Type: $contentType",

"",

$fileContent,

"--$boundary--"

) -join "`r`n"



# Make the API request to upload the file

$response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $headers -ContentType "multipart/form-data; boundary=$boundary" -Body $body



uploadUrl “https://upload.box.com/api/2.0/files/content


function with content types


function Get-ContentType {


param (


string]$filePath


)



$extension = nSystem.IO.Path]::GetExtension($filePath).ToLower()



switch ($extension) {

".txt" { return "text/plain" }

".pdf" { return "application/pdf" }

".doc" { return "application/msword" }

".docx" { return "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }

".xls" { return "application/vnd.ms-excel" }

".xlsx" { return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }

default { return "application/octet-stream" }

}



}



I understand that the problem is where I get content or encode it, I tried different ways, something like this


$fileStream = mSystem.IO.File]::OpenRead($filePath) # Create a stream reader to read the file content


$streamReader = rSystem.IO.StreamReader]::new($fileStream, aSystem.Text.Encoding]::UTF8)


$fileContent = $streamReader.ReadToEnd()



But still small progress. The files I am uploading are a small files, nothing extraordinary, they something like samples.

Hi @avetatev , welcome to the forum!



I’m not a Powershell expert, and building a form-data request by hand is always tricky.



Let’s see if there is anyone else in this forum that can help you.



Having said that, there is another option that perhaps you can consider, if it fits your use case.



We provide a Box CLI, that integrates very well with powershell or bash scripts, and will take care of the upload for you.



For example to upload a file it would look like this:



box files:upload /path/to/file.pdf --parent-id 22222



You can take a look at:





We also have a quick start guide:





Let us know if this is a possibility you would consider, and we can take a deeper dive.



Best regards


Thanks,


Can I work with Box CLI if I have given access token? It is saved in the toml file and I get it and pass to API. How it is going to work with CLI? Is it required to login with clients id and secret every time?


Hi @avetatev



I don’t know how are you obtaining an access token, assuming you do get a fresh one each time (access tokens only last for 60 minutes) then you can use the token directly on the CLI typically passing it as a parameter -t or --token=.



The other option is to have an environment (or multiple) configured on the CLI it self.


You can have OAuth, CCG, or JWT, and typically for a script you would choose either CCG or JWT.



To setup the Box CLI for JWT authentication, follow this guide.



To setup for CCG check out the details on the box configure:environments:add command.



Once the environments are created you can set a default environment or select which one you want to use in your script.



Best regards


Reply