How to download view only files from OneDrive

OneDrive is a famous cloud storage service which is provided by Microsoft. It is an alternative to Google Drive and that is the reason why both of these competitors try to provide the best features which are not available in other’s provided services. 

OneDrive, being similar to Google Drive, gives you a lot of features for storing your data and information on cloud storage. You can share this stored data with anyone so that the person you have shared the link with, can also access your files. 

When talking about sharing feature of OneDrive, you can share a file or multiple files in different formats to anyone. Sometimes, the person who is sharing a OneDrive file does not give us permission to edit the file and sometimes he shares a file with view only status.

In this blog post we will discuss how we can download a view only file from OneDrive.

In simple, to download a view only file from OneDrive you will have to use JavaScript code. Let us see how we can.

Downloading a view only file from OneDrive

A view only file on OneDrive is a file which is shared with you with a status of view only. It means that you can see the content of the file but you can’t edit this file and you can’t download the file as the download option is not available for such type of file. 

Common sharing features available in OneDrive include:

  • Can be edited: This type of file can be edited by the person who has access to this file.
  • Can’t be edited: This type of file can be viewed but can’t be edited.
  • View only: This type of file can be viewed but can’t be downloaded.
Disclaimer: The method discussed in this blog post must be used only for educational purposes as it is not good to download a file without someone’s permission.

In general, when you open a file with view only credential by tapping on the link which has been shared with you by the owner then you see that you can’t download the file nor you can print it.

So the question here arises is how we will download it. We will just make use of JavaScript code in our browser and that code will generate copies of the pages of that file and then it will combine all those copies in a single file and give the required file to us. We will make use of console for this as we did for checking answers of Chegg. 

Downloading a view only PDF file from OneDrive

Let us think that you have been given a link to a PDF file which is stored on OneDrive and shared with you with view only credential then what you have to do is:

  • Open the File on your browser. For example we open it on Chrome.
  • The download and print option is not available in the file as you will see there.
  • Open developer tools on the browser. For chrome tap on three dots located in the upper right corner >> More tools >> Developers tools >> Console >> Clear console. Then add the following JS code:
// Create a new script element
const jspdfScript = document.createElement(“script”);
// Function to execute once the script has been loaded
jspdfScript.onload = function () {
  // Create a new instance of jsPDF  
const pdf = new jsPDF();
  // Get all the images on the page  
const elements = document.getElementsByTagName(“img”);
  // Loop through all the images 
 for (let i = 0; i < elements.length; i++) {    const img = elements[i];
    // Check if the image is a blob URL, if not, skip it  
  if (!/^blob:/.test(img.src)) {      continue;    }
    // Create a new canvas element   
 const canvasElement = document.createElement(‘canvas’);    const context = canvasElement.getContext(‘2d’);
    // Set the canvas dimensions to match the image  
  canvasElement.width = img.width;    canvasElement.height = img.height;
    // Drawing the image onto the canvas 
   context.drawImage(img, 0, 0, img.width, img.height);
    // Converting the canvas data to a JPEG data URL 
   const imgData = canvasElement.toDataURL(‘image/jpeg’, 1.0);
    // Adding the image to the PDF document 
   pdf.addImage(imgData, ‘JPEG’, 0, 0);
    // Adding a new page to the PDF document  
  pdf.addPage();  }
  // Saveing the PDF document as file named “download.pdf” 
 pdf.save(‘download.pdf’);};
// Setting out the script source to the jsPDF library on Cloudflare
jspdfScript.src = ‘https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js’;
// Adding script element to the HTML document’s body
document.body.appendChild(jspdfScript);
// make required changes as you like

I have added comment section in each line of code so that it will be easy for you to understand the code.

placing code in console
  • Once you have run the code it will start downloading the PDF file. 

Downloading view only video file from OneDrive

We can record the video by using a on screen recorder or any third party tool as it will be easy method but you can also try out this alternative:

  • Run this code in Console
// You can also make the required changes if you like// Create a new script elementconst
jspdfScript = document.createElement(“script”);
// Function to execute once the script has been loaded
jspdfScript.onload = function () {
  // Create a new instance of jsPDF 
 const pdf = new jsPDF();
  // Get the video element from the page 
 const video = document.getElementsByTagName(“video”)[0];
  // Create a new canvas element to draw the video frames onto 
 const canvasElement = document.createElement(“canvas”);  const context = canvasElement.getContext(“2d”);
  // Setting out the canvas dimensions to match the video 
 canvasElement.width = video.videoWidth;  canvasElement.height = video.videoHeight;
  // Creating a new MediaRecorder instance and set the format to ‘video/webm’  
const mediaRecorder = new MediaRecorder(video.captureStream(), { mimeType: ‘video/webm’ });  let chunks = [];
  // Start recording when the MediaRecorder is ‘dataavailable’ 
 mediaRecorder.addEventListener(“dataavailable”, event => {    chunks.push(event.data);  });
  // Stop recording and create the video file when the MediaRecorder stops 
 mediaRecorder.addEventListener(“stop”, event => {    const blob = new Blob(chunks, { type: ‘video/webm’ });    const videoURL = URL.createObjectURL(blob);
    // Add video file to the PDF document   
 pdf.addFileToVFS(“video.webm”, blob);    pdf.addPage();    pdf.addImage(videoURL, “WEBP”, 0, 0, canvasElement.width, canvasElement.height);    pdf.save(‘download.pdf’);
    // Cleaning up after recording is complete  
  chunks = [];    URL.revokeObjectURL(videoURL);  });
  // Start rcording when the user clicks on the video element 
 video.addEventListener(“click”, () => {    mediaRecorder.start();  });
  // Stopping the recording when the user pauses or ends the video 
 video.addEventListener(“pause”, () => {    mediaRecorder.stop();  });  video.addEventListener(“ended”, () => {    mediaRecorder.stop();  });};
// Set script source to the jsPDF library on Cloudflare
jspdfScript.src = ‘https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js’;
// Adding script element to the HTML document’s body
document.body.appendChild(jspdfScript);

This code is modified form of the previous code and it will help in recording and downloading that video file.

Downloading view only audio file

Similarly you can change video function with AudioContext function to record an audio file and that file will be downloaded easily. There are also different types of files available so I would like to suggest you to make changes in the code according to your need and type of file. 

Can we download a file from OneDrive which is shared with us having “Can’t be edited” credential?

Yes! We can download a file which does not allow us to make changes as the download option will be available for such type of file.

Conclusion

We hope that the above discussed method will be helpful for you and you will be able to download view only OneDrive file whether it is PDF, Audio or Video.