3

Can we upload a file to any default folder, say Documents or Public folder, in OneDrive using OneDrive's JavaScript File Picker API?

i.e. instead of setting path using

WL.upload({                           
    path: response.data.folders[0].id,                          
    element: "file",
    overwrite: "rename"
});

can we set the path value for a default folder like Documents/Public?

grg
  • 5,023
  • 3
  • 34
  • 50
Dipti Sheth
  • 159
  • 1
  • 2
  • 11

2 Answers2

1

Following the steps on https://msdn.microsoft.com/en-us/library/hh550848.aspx will allow you to accomplish this task.

On the HTML portion of your code, add the and to call the wl.upload function. Below is my code that will allow the use to select the file and upload it to a default folder on OneDrive. In this case, I used "me/skydrive/my_documents"

<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Code Sample</title>
        <script type="text/javascript" src="//js.live.net/v5.0/wl.js"></script>
    </head>
    <body>
<div style="padding: 1em">


        <div id="signin"></div>
        <label id="info"></label>
    <form>
    <input id="file" name="file" type="file" />
</form>
    <button onclick="uploadFile()">Save file directly (calling WL.upload)</button>
        <script>
            WL.init({
                client_id: 'Your_Client_ID',
                redirect_uri: 'Your_Redirect_URL',
                scope: "wl.signin",
                response_type: "token"
            });
            WL.ui({
                name: "signin",
                element: "signin"
            });
            function uploadFile() {
                WL.login({
                    scope: "wl.skydrive_update"
                }).then(
                    function (response) {
                        WL.upload({
                            path: "me/skydrive/my_documents",
                            element: "file",
                            overwrite: "rename"
                        }).then(
                            function (response) {
                                document.getElementById("info").innerText =
                                    "File uploaded.";
                            },
                            function (responseFailed) {
                                document.getElementById("info").innerText =
                                    "Error uploading file: " + responseFailed.error.message;
                            }
                        );
                    },
                    function (responseFailed) {
                        document.getElementById("info").innerText =
                            "Error signing in: " + responseFailed.error.message;
                    }
                );
            }

        </script> 
    </div>
    </body>
</html> 
Toan-Nguyen
  • 321
  • 1
  • 3
0

The path "response.data.folders[0].id" is used to select the folder that the user has selected from the OneDrive file picker when WL.fileDialog is called. If you are uploading to a default folder, you would want to omit the file picker and use the JavaScript API.

Toan-Nguyen
  • 321
  • 1
  • 3
  • I'm using javascript api of one-drive to upload file ..but I want to set one default folder so that end user can't choose any folder and file will be uploaded to one default folder. Can you please guide me on this further? – Dipti Sheth Mar 17 '15 at 01:35
  • Sorry about that. In this case, you will want to your app to call the WL.upload function directly and call out the path that you want the default folder to be. In the example on https://msdn.microsoft.com/en-us/library/hh550848.aspx, you can change the path to "me/skydrive/my_documents" or any friendly folder name. You will have to create an tag for the file info and a button to call the function. – Toan-Nguyen Mar 18 '15 at 08:03
  • I changed path to "me/skydrive/my_documents" ...but it still shows user's one drive folder structure, showing default folders like Documents, Pictures, public with options to Save, Cancel and New Folder. Can't it directly upload file to Documents folder without giving option to choose any other folder? – Dipti Sheth Mar 19 '15 at 09:01
  • The picker will always display the folder options for the user. So if you want files to be uploaded to a specific folder without giving the user an option, you'll need to call the WL.upload function directly. – Toan-Nguyen Mar 20 '15 at 17:31
  • Can you please give me example how to call WL.upload function directly without giving user any option to select folder? – Dipti Sheth Mar 23 '15 at 00:15