Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy Issue with GetBuffer of a file with PNP-JS #61

Closed
Korigoth opened this issue Aug 7, 2018 · 6 comments
Closed

Proxy Issue with GetBuffer of a file with PNP-JS #61

Korigoth opened this issue Aug 7, 2018 · 6 comments
Assignees

Comments

@Korigoth
Copy link

Korigoth commented Aug 7, 2018

We have an issue while using PNP-JS and SP-Rest-Proxy.

We are trying to get a Template we have created (Content-Type) (docx).
And upload it in it's content library.

The file uploaded in the lib is always corrupted.

I doesn't know if it the getBuffer() method that get a corrupted stream
or the add() method that corrupt the item while being uploaded with the proxy

Here is the code we use for the file

templateUrl = '/sites/App/Template/Forms/Template/test.docx'
name = 'test.docx'
depositUrl = '/sites/App/Template'

this.web = new Web('http://localhost:8080/sites/App');
this.web.getFileByServerRelativeUrl(this.templateUrl)
    .getBuffer()
    .then((templateData: ArrayBuffer) => { 
        this.web.getFolderByServerRelativeUrl(depositUrl).files.add(name, templateData)); 
});

Here is the code we use for the Proxy Settings

var RestProxy = require('sp-rest-proxy');

const settings = {
    configPath: './sharepoint/config/private.json', // Location for SharePoint instance mapping and credentials
    port: 8080,                                     // Local server port
    staticRoot: './static'                          // Root folder for static content
};

const restProxy = new RestProxy(settings);
restProxy.serve();

The issue discussion started here => pnp/pnpjs#196

@koltyakov koltyakov self-assigned this Aug 7, 2018
@koltyakov
Copy link
Owner

koltyakov commented Aug 7, 2018

Hey @Korigoth,

Experiencing issues with making .getBuffer to work correctly in a limited timeframes. Got to go through thoughtfully when I have a chance.

But, I can offer another approach for achieving what you need in your original question in PnPjs repo, which works with the proxy (the method even faster as it copies files on server-side):

const templateUrl = '/sites/site/Templates/Template.xlsx';
const targetUrl = '/sites/site/Documents/NewDocument.xlsx';

web.getFileByServerRelativeUrl(templateUrl)
  .copyTo(targetUrl)
  .then(templateData => {
    return web.getFileByServerRelativeUrl(targetUrl).getItem();
  })
  .then(item => {
    return item.validateUpdateListItem([
      { FieldName: 'Title', FieldValue: 'Some title' }
    ], true);
  })
  .then(console.log);

Also, please update sp-rest-proxy dependency to the latest version - 2.7.3.

@Korigoth
Copy link
Author

Korigoth commented Aug 8, 2018

@koltyakov
i got an error but it seems to be about where we store our document

error:
You can not copy or move files between the Forms directory and a document library. Copy the file offsite first, and then download it from this location.

/sites/app/template/Forms/document/test.docx
to
/sites/app/template/test.docx

so in our case, i think the method getBuffer() is the way to go

@koltyakov
Copy link
Owner

@Korigoth,

I was able to make getBuffer works.
Now, adding some tests and going to publish an update.
Will inform here when a new version is published.

@koltyakov
Copy link
Owner

The update is published, ver 2.7.4.

@Korigoth
Copy link
Author

@koltyakov
work perfectly now with our .docx!
thanks for the fix!

@koltyakov
Copy link
Owner

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants