How do I save documents locally using "save_url" method? Any sample codes available?

How do I save documents locally using "save_url" method? Any sample codes available?

A "save_url" is a publicly accessible Web hook or a Web URL to which Zoho will push the updated document content from its Office editors (Writer, Sheet, Show) to your storage server.

In order to make use of "save_url" method and save document locally, business embedding our Online Office editors in their Web application should fulfil certain requirements as listed below:
  1. Business need to expose one of the remote server ports - port 443 (HTTPS) or port 80 (HTTP) - from their location, for Zoho Office editors (Writer, Sheet, Show) to push the document data and save it locally.

  2. The "save_url" parameter value specified in the Office Integrator API request should to be a proper domain name and publicly accessible over the web. Examplehttps://document.zylker.com/save.php

Save_url Sample Code:

Once you are done with editing your document and click "Save", the respective Zoho Office editors (Writer, Sheet, Show) will convert the document to the required format (say docx/xlsx/pptx) as specified in the API request and push the modified content to your storage server mentioned in the "save_url". We have come up with save_url sample codes in different languages (PHP, Java, ASP.NET, etc.) that illustrates how a save_url file should look like.

PHP Sample Code:

Developers who are using PHP programming language for their Zoho Office Integrator solution, can use the below save_url sample code and save the document locally in their own storage servers.

'/DOC_REPOSITORY_FOLDER_PATH/samplefile.docx';
$tmp_filename = $_FILES['content']['tmp_name']; 
$upload_status = move_uploaded_file($tmp_filename, $filepath);
?>           
Code Reference:

$_FILES['content']['tmp_name'] - $_FILES array is where PHP stores all the information about files.
There are two elements of this array - content and tmp_name

content - document content that is uploaded to Zoho editors while doing a HTTP multi-part form POST.
tmp_name - tmp_name contains the path to the temporary file that resides on the Zoho document server. 

move_uploaded_file - Thisfunction moves the temporary file from 
Zoho's document server to the document repository folder path that you provide 
along with the file name and file extension.

Note: Example of doc repository folder path - "/apache/htdocs/zohosave/samplefile.docx" 
or "C:\Zylker\Documents\samplefile.docx"

Java Sample Code
:

In the following example, a sample action (savedoc.do) written using Struts Application will parse, retrieve and save the document content sent from Zoho Office editors (Writer, Sheet, Show) to the folder path of your local storage server specified in the save_url parameter.

package com.Sample;
import java.lang.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//add this jar - commons-fileupload-1.3.3.jarimport org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public classSaveUrlServletextendsHttpServlet {
private static final long serialVersionUID = 1L;
private static finalString UPLOAD_DIRECTORY = "upload";
private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  
// 3MBprivate static final int MAX_FILE_SIZE      = 1024 * 1024 * 40;
// 40MBprivate static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50;
// 50MBpublic void doPost(HttpServletRequest request,
HttpServletResponse response) throwsServletException, IOException {
PrintWriter writer = response.getWriter();
if (!ServletFileUpload.isMultipartContent(request)) {

writer.write("RESPONSE: Form must has enctype=multipart/form-data.");

response.setStatus(response.SC_BAD_REQUEST);
writer.flush();
return;
}
DiskFileItemFactory factory = newDiskFileItemFactory();

factory.setSizeThreshold(MEMORY_THRESHOLD);

factory.setRepository(newFile(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = newServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
String uploadPath = getServletContext().getRealPath("")+ File.separator + UPLOAD_DIRECTORY;
File uploadDir = newFile(uploadPath);
if (!uploadDir.exists()) {
 uploadDir.mkdir();
}

try {
List formItems = upload.parseRequest(request);
System.out.println("Form Items Values   >>>   "+formItems);
if (formItems != null && formItems.size() > 0) {

for (FileItem item : formItems) {
System.out.println("Field Name:"+item.getFieldName());
if(!item.isFormField()){
if (item.getFieldName().equalsIgnoreCase("content")){
String  fileName = newFile(item.getName()).getName();
String filePath = uploadPath + "/" + fileName;
File storeFile = newFile(filePath);
item.write(storeFile);
}
}else{
byte[] fieldVal;
if (item.getFieldName().equalsIgnoreCase("filename")){
fieldVal = item.get();
String fileNameValue = newString(fieldVal);
System.out.println(item.getFieldName()+" value: "+fileNameValue);
}else if(item.getFieldName().equalsIgnoreCase("id")){
fieldVal = item.get();
String idValue = newString(fieldVal);
System.out.println(item.getFieldName()+" value: "+idValue);
}
else if(item.getFieldName().equalsIgnoreCase("format")){
fieldVal = item.get();
String formatValue = newString(fieldVal);
System.out.println(item.getFieldName()+" value: "+formatValue);
}
}
}
response.setStatus(response.SC_OK);
writer.write("Document Saved Successfully!");
}
}catch (Exception ex) {
response.setStatus(response.SC_BAD_REQUEST);

writer.write("RESPONSE:Document save fails");
// For Custom message showing in Zoho editor

}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throwsServletException, IOException {
PrintWriter writer = response.getWriter();
writer.write("Not Allowed");
response.setStatus(response.SC_BAD_REQUEST);
}
}

ASP.NET Sample Code:

Similarly, the save_url sample file in ASP.NET will look like the one below:

'this is page load event 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load'

declaring variable - object declared to get the content of uploaded file

Dim m_objFile As HttpPostedFileDim filename As String'here we are getting the requested file content 

 m_objFile = Request.Files("content")'

 Here we are getting the exact file name that was sent during HTTP POST form submit

filename = Request.QueryString("filename")

'saving the file in document repository folder path

Request.Files("content").SaveAs("C:\inetpub\" + filename) 

End Sub

Note: Any text inside the sample code preceded by single quotes (') is a comment in ASP.Net.


    • Related Articles

    • How do I edit documents in Zoho Writer? Any sample codes?

      To edit an existing document from your web application, you need to make use of Zoho Writer's Edit Document API. HTML Sample Code: <form method="POST" action=" https://writer.zoho.com/writer/officeapi/v1/document" enctype="multipart/form-data" ...
    • How do I compare two documents in Zoho Writer? Any sample codes?

      To compare two versions of a document, you need to make use of Zoho Writer's Comparison API. HTML Sample Code: <form method="POST" action="https://writer.zoho.com/writer/officeapi/v1/document/compare" enctype="multipart/form-data" ...
    • Sample Codes - Java SDK

      All of Zoho CRM's APIs can be used through the Java SDK, to enable your custom application to perform data sync to the best degree. Here are the sample codes for all the API methods available in our SDK. Rest Client Operations These methods involve ...
    • Sample Codes - Python SDK

      All of Zoho CRM's APIs can be used through the Python SDK, to enable your custom application to perform data sync to the best degree. Here are the sample codes for all the API methods available in our SDK. Rest Client Operations These methods involve ...
    • Sample Codes - C# SDK

      All of Zoho CRM's APIs can be used through the C# SDK, to enable your custom application to perform data sync to the best degree. Here are the sample codes for all the API methods available in our SDK. Rest Client Operations These methods involve ...