Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,26 @@ client
})

````
### `.put()`
Use `.put()` to upload files to Microsoft Graph with streams.
### `.put()` and `.putStream()`

You can upload files to the graph using `.put()`. For example, this can be used to update a profile picture from an HTML input form. See the [browser sample](samples/browser) for complete code.

```javascript
var file = document.querySelector('input[type=file]').files[0];

client
.api('/me/photo/$value')
.put(file, (err, res) => {
if (err) {
console.log(err);
return;
}
console.log("We've updated your picture!");
});
```


Use `.putStream()` to upload files to Microsoft Graph with Node.js streams.
````javascript

// Upload a file to OneDrive
Expand Down
9 changes: 8 additions & 1 deletion lib/graph-js-sdk-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ class GraphRequest {
.post(url)
.send(content), callback);
}
put(content, callback) {
let url = this.buildFullUrl();
return this.sendRequestAndRouteResponse(request
.put(url)
.type('application/octet-stream')
.send(content), callback);
}
create(content, callback) {
return this.post(content, callback);
}
Expand Down Expand Up @@ -234,7 +241,7 @@ class GraphRequest {
}
});
}
put(stream, callback) {
putStream(stream, callback) {
this.config.authProvider((err, accessToken) => {
if (err === null && accessToken !== null) {
let url = this.buildFullUrl();
Expand Down
3 changes: 2 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export declare class GraphRequest {
delete(callback?: GraphRequestCallback): Promise<any> | void;
patch(content: any, callback?: GraphRequestCallback): Promise<any> | void;
post(content: any, callback?: GraphRequestCallback): Promise<any> | void;
put(content: any, callback?: GraphRequestCallback): Promise<any> | void;
create(content: any, callback?: GraphRequestCallback): Promise<any> | void;
update(content: any, callback?: GraphRequestCallback): Promise<any> | void;
del(callback?: GraphRequestCallback): Promise<any> | void;
Expand All @@ -63,7 +64,7 @@ export declare class GraphRequest {
private routeResponseToCallback(requestBuilder, callback);
private sendRequestAndRouteResponse(requestBuilder, callback?);
getStream(callback: GraphRequestCallback): void;
put(stream: any, callback: Function): void;
putStream(stream: any, callback: Function): void;
private configureRequest(requestBuilder, accessToken);
getResultIterator(): IterableIterator<(callback: any) => void>;
query(queryDictionaryOrString: string | {
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

34 changes: 32 additions & 2 deletions samples/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,42 @@
}
console.log(res);
});

function updateProfilePicture() {
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();

reader.addEventListener("load", function () {
client
.api('/me/photo/$value')
.put(file, (err, res) => {
if (err) {
console.log(err);
return;
}
console.log("We've updated your picture!");
});
}, false);

if (file) {
reader.readAsDataURL(file);
}
}
</script>
</head>
<body>
Open your browser JavaScript console to see the result!

<p>
By loading this page, we called /me endpoint on the Microsoft Graph. Open your browser JavaScript console to see the result!
</p>
<br>

<p>
Also, you can update your profile picture:
<input type="file" onchange="updateProfilePicture()"><br>
</p>
<br>
<p>Please check the Node.js sample for more examples on calling the Graph. This sample is just for how to include the SDK in an HTML page</p>

Please check the Node.js sample for tons of examples on calling the graph. This sample is just for how to include the sdk in an HTML page
</body>
</html>
4 changes: 2 additions & 2 deletions samples/node/node-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ client
let photoReadStream = fs.createReadStream('../logo.png');
client
.api('/me/drive/root/children/logo234.png/content')
.put(photoReadStream, (err) => {
.putStream(photoReadStream, (err) => {
console.log(err);
});

Expand All @@ -277,7 +277,7 @@ let profilePhotoReadStream = fs.createReadStream('me.jpg');

client
.api('/me/photo/$value')
.put(profilePhotoReadStream, (err) => {
.putStream(profilePhotoReadStream, (err) => {
if (err) {
console.log(err);
return;
Expand Down
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@ export class GraphRequest {
);
}

put(content:any, callback?:GraphRequestCallback):Promise<any>|void {
let url = this.buildFullUrl();
return this.sendRequestAndRouteResponse(
request
.put(url)
.type('application/octet-stream')
.send(content),
callback
);
}

// request aliases
// alias for post
create(content:any, callback?:GraphRequestCallback):Promise<any>|void {
Expand Down Expand Up @@ -361,7 +372,7 @@ export class GraphRequest {
});
}

put(stream:any, callback:Function) {
putStream(stream:any, callback:Function) {
this.config.authProvider((err, accessToken) => {
if (err === null && accessToken !== null) {
let url = this.buildFullUrl();
Expand Down