Does SharePoint Online support storing empty (0-Byte) files?

Valentin 81 Reputation points
2020-11-12T16:53:36.22+00:00

I'll refer to my original StackOverflow post, you can find here: https://stackoverflow.com/questions/64772346

I'm developing a node service, that pushes file to SharePoint online.
We are using the official Graph SDK for JavaScript (..and Types).

so far - everything's fine, but if the folder contains a file that has 0-length, I can not find a way to push these files to the drive.

If I try to use the MicrosoftGraph.LargeFileUploadTask JavaScript Class or the OneDriveLargeFileUploadTask the upload doesn't even start, because the formatting of the "Content-Length" header in uploadSlice() fails.

We can skip the class's code and just upload an empty slice by ourself (all code only applies to empty files):

uploadedFile = await client  
    .api(uploadSession.url)  
    .headers({  
        "Content-Length": `0`,  
        "Content-Range": `bytes 0-0/0`,  
    })  
    .put(file); //file is an empty Buffer  

Results in: Error: The Content-Range header is missing or malformed

I've tried to commit the empty file without uploading a slice: according to https://learn.microsoft.com/de-de/graph/api/driveitem-createuploadsession?view=graph-rest-1.0#completing-a-file (I've explictly set deferCommit=true for the upload session)

After the final byte range of the file is PUT to the upload URL, send a final POST request to the upload URL with zero-length content (currently only supported on OneDrive for Business and SharePoint).

uploadedFile = await client.api(requestUrl)  
    .headers({  
        "Content-Length": `0`,  
    })  
    .post(null);  

No Luck, this is the response: SharePoint / Graph expects the missing bytes of unkown lenght, I think:

{  
    "@odata.context":"...",  
    "expirationDateTime":"2020-11-10T16:04:31.594Z",  
    "nextExpectedRanges":["0-"],  
    "uploadUrl":"..."  
}  

The file is not present.

I can not find any documentation on how to handle empty files with Microsoft Graph / SharePoint. I've thought about creating an empty file like using touch online, instead of uploading it, but I can not find a way to do that either.

  1. you CAN'T upload an empty file via the SharePoint Online GUI: "Sorry, this "Documents" library can't upload empty folders or empty files. Please try again."
  2. You CAN upload an empty file to an SPO library via the OneDrive for Windows Client.

So I'm searching for an official source that can tell me wether 0-byte files in SharePoint Online are generally supported or not. And if yes - under which circumstances.

Thank you!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,618 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Deva-MSFT 2,256 Reputation points Microsoft Employee
    2020-11-16T16:44:54.327+00:00

    Per documentation, I remember this -

    your app must ensure the total file size specified in the Content-Range header is the same for all requests. If a byte range declares a different file size, the request will fail.

    0 comments No comments