使用 jQuery 将 PDF 保存到 OneDrive

Saving PDF to OneDrive using jQuery

我的公司开始使用 Microsoft Office 365。他们正在使用 OneDrive 来保存和查看文件。

我的任务是建立一个允许客户将文件直接上传到 OneDrive 的网站。我的团队创建了我们自己的 OneDrive 虚拟帐户来测试保存将由客户上传的文件。

从简单的 HTML 表单输入开始:

<form id="uploadfile">
  <input type="file" id="pdfFile" name="pdfFile" class="form-control" />
  <button type="button" class="btn btn-primary" id="uploadBtn">Upload File</button>
</form>

现在 jQuery:

$('#uploadBtn').on('click', function(e)
{
  e.preventDefault();
  var formData = new FormData($('#uploadfile')[0]);  // <-- not currently using this variable, but I know I will need it
  var filename = $('#pdfFile').val().replace(/.*(\/|\)/, '');

  var odOptions = 
  {
    clientId: "00000000-xxxxx-0000-xxxx-0000000000",
    action: "upload",  // <-- was originally set to save
    sourceInputElementId: "pdfFile",
    sourceUri: "",
    fileName: filename,
    openInNewWindow: true,
    advanced: {endpointHint: "api.onedrive.com"},
    success: function(files) { /* success handler */ },
    progress: function(p) { /* progress handler */ },
    cancel: function() { /* cancel handler */ },
    error: function(e) { /* error handler */ }
  }
  OneDrive.save(odOptions);
});

以上全部来自以下link:

https://docs.microsoft.com/en-us/onedrive/developer/controls/file-pickers/js-v72/save-file

编辑

自问这个问题以来,我已经取得了一些进展。我可以到达 OneDrive window 打开的位置,并且可以看到各种文件夹。但是好像没有我要保存的文件。

从这里开始:

用户选择了一张图片。单击上传按钮后,OneDrive 打开:

但是当我在 OneDrive 中时 window,点击保存按钮后,没有文件被保存。

我缺少什么可以将文件保存到 OneDrive 中?

我终于可以保存到 OneDrive 了。在应用程序注册门户(您可以在其中检索您的 appID)上进行了一些调整。我添加了权限以确保我可以 read/write 到 OneDrive 中的文件夹。

我还对代码的odOptions部分进行了调整:

$('#uploadBtn').on('click', function(e)
{
  e.preventDefault();
  //var formData = new FormData($('#uploadfile')[0]); 
  //var filename = $('#pdfFile').val().replace(/.*(\/|\)/, '');

  var odOptions = 
  {
    clientId: "00000000-xxxxx-0000-xxxx-0000000000",
    action: "save",  // upload was not valid
    sourceInputElementId: "pdfFile",
    //sourceUri: "", 
    //fileName: filename,
    openInNewWindow: true,
    advanced: {endpointHint: "api.onedrive.com"},
    success: function(files) { /* success handler */ },
    progress: function(p) { /* progress handler */ },
    cancel: function() { /* cancel handler */ },
    error: function(e) { /* error handler */ }     
  }
  OneDrive.save(odOptions);
});

按照此处的文档帮助解决了我的问题:

https://docs.microsoft.com/en-us/onedrive/developer/controls/file-pickers/js-v72/save-file