Upload File
Purpose
To upload a CSV file in ZIP format for bulk write API. The response contains the file_id. Use this ID while making the bulk write request.
For modules that have subforms, line items, multi-select lookup, and multi-user lookup, you can zip multiple CSV files together. For example, if your Leads module has a subform, then Leads is the parent and subform is the child. So, you must prepare two CSV files and zip them together to import data from both in a single bulk write job.
Endpoints
Request Details
Request URL
https://content.zohoapis.com/crm/v2.1/upload
Header
Authorization: Zoho-oauthtoken d92d4xxxxxxxxxxxxx15f52
feature: bulk-write - To indicate this is a bulk write job
X-CRM-ORG: zgid - The unique ID (zgid) of your organization obtained through the Organization API
Scope
scope=ZohoFiles.files.ALL
Input form-data
- filefile, mandatory
Choose the zip file you want to upload.
Sample Request
Copiedcurl "https://content.zohoapis.com/crm/v2.1/upload"
-X POST
-H "Authorization: Zoho-oauthtoken 1000.8cb99dxxxxxxxxxxxxx9be93.9b8xxxxxxxxxxxxxxxf"
-H "X-CRM-ORG: 56xxxx47"
-H "feature: bulk-write"
-F "file=@file.csv.zip"
Copied//Get instance of BulkWriteOperations Class
$bulkWriteOperations = new BulkWriteOperations();
//Get instance of FileBodyWrapper class that will contain the request file
$fileBodyWrapper = new FileBodyWrapper();
//Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
$streamWrapper = new StreamWrapper(null, null, $absoluteFilePath);
// $file = fopen($absoluteFilePath, "rb");
// $stream = fread($file, filesize($absoluteFilePath));
// fclose($file);
//Get instance of StreamWrapper class that takes file name and stream of the file to be attached as parameter
// $streamWrapper = new StreamWrapper(basename($absoluteFilePath), $stream);
//Set file to the FileBodyWrapper instance
$fileBodyWrapper->setFile($streamWrapper);
//Get instance of HeaderMap Class
$headerInstance = new HeaderMap();
//To indicate that this a bulk write operation
$headerInstance->add(UploadFileHeader::feature(), "bulk-write");
$headerInstance->add(UploadFileHeader::XCRMORG(), $orgID);
//Call uploadFile method that takes FileBodyWrapper instance and headerInstance as parameter
$response = $bulkWriteOperations->uploadFile($fileBodyWrapper, $headerInstance);
Copied<?php
class UploadFile
{
public function execute(){
$curl_pointer = curl_init();
$curl_options = array();
$curl_options[CURLOPT_URL] = "https://content.zohoapis.com/crm/v2.1/upload";
$curl_options[CURLOPT_RETURNTRANSFER] = true;
$curl_options[CURLOPT_HEADER] = 1;
$curl_options[CURLOPT_CUSTOMREQUEST] = "POST";
$fileName = "asdad.zip";
$filePath = "/Users/test/PHP/PHPNativeSampleCode/asdad.zip";
$file = fopen($filePath, "rb");
$fileData = fread($file, filesize($filePath));
$date = new \DateTime();
$current_time_long= $date->getTimestamp();
$lineEnd = "\r\n";
$hypen = "--";
$contentDisp = "Content-Disposition: form-data; name=\""."file"."\";filename=\"".$fileName."\"".$lineEnd.$lineEnd;
$data = utf8_encode($lineEnd);
$boundaryStart = utf8_encode($hypen.(string)$current_time_long.$lineEnd) ;
$data = $data.$boundaryStart;
$data = $data.utf8_encode($contentDisp);
$data = $data.$fileData.utf8_encode($lineEnd);
$boundaryend = $hypen.(string)$current_time_long.$hypen.$lineEnd.$lineEnd;
$data = $data.utf8_encode($boundaryend);
$curl_options[CURLOPT_POSTFIELDS]= $data;
$headersArray = array();
$headersArray = ['ENCTYPE: multipart/form-data','Content-Type:multipart/form-data;boundary='.(string)$current_time_long];
$headersArray[] = "content-type".":"."multipart/form-data";
$headersArray[] = "feature".":"."bulk-write";
$headersArray[] = "X-CRM-ORG".":"."680100987";
$headersArray[] = "Authorization". ":" . "Zoho-oauthtoken " ."1000.30f3a589XXXXXXXXXXXXXXXXXXX4077.dc5XXXXXXXXXXXXXXXXXXXee9e7c171c";
var_dump($data);
$curl_options[CURLOPT_HTTPHEADER]=$headersArray;
curl_setopt_array($curl_pointer, $curl_options);
$result = curl_exec($curl_pointer);
$responseInfo = curl_getinfo($curl_pointer);
curl_close($curl_pointer);
list ($headers, $content) = explode("\r\n\r\n", $result, 2);
if(strpos($headers," 100 Continue")!==false){
list( $headers, $content) = explode( "\r\n\r\n", $content , 2);
}
$headerArray = (explode("\r\n", $headers, 50));
$headerMap = array();
foreach ($headerArray as $key) {
if (strpos($key, ":") != false) {
$firstHalf = substr($key, 0, strpos($key, ":"));
$secondHalf = substr($key, strpos($key, ":") + 1);
$headerMap[$firstHalf] = trim($secondHalf);
}
}
$jsonResponse = json_decode($content, true);
if ($jsonResponse == null && $responseInfo['http_code'] != 204) {
list ($headers, $content) = explode("\r\n\r\n", $content, 2);
$jsonResponse = json_decode($content, true);
}
var_dump($headerMap);
var_dump($jsonResponse);
var_dump($responseInfo['http_code']);
}
}
(new UploadFile())->execute();
Copiedbwo = BulkWrite::BulkWriteOperations.new
# Get instance of FileBodyWrapper class that will contain the request file
file_body_wrapper = BulkWrite::FileBodyWrapper.new
# """
# StreamWrapper can be initialized in any of the following ways
# * param 1 -> fileName
# * param 2 -> Read Stream.
# * param 3 -> Absolute File Path of the file to be attached
# """
# stream_wrapper = StreamWrapper.new(name,stream,absolutefilepath)
sw = Util::StreamWrapper.new(nil, nil, '/Users/user_name/RUBYWS/zohocrm-rubysdk-sample-application/3524033000005787003.zip')
# Set file to the FileBodyWrapper instance
file_body_wrapper.file = sw
# Get instance of HeaderMap Class
header_instance = HeaderMap.new
# Possible headers for upload_file operation
header_instance.add(BulkWrite::BulkWriteOperations::UploadFileHeader.feature, 'bulk-write')
header_instance.add(BulkWrite::BulkWriteOperations::UploadFileHeader.X_crm_org, org_id)
# Call upload_file method that takes FileBodyWrapper instance and header_instance as parameter
response = bwo.upload_file(file_body_wrapper, header_instance)
Copiedrequire 'net/http'
require 'json'
class UploadFile
def execute
url = "https://content.zohoapis.com/crm/v2.1/upload"
url = URI(url)
req = Net::HTTP::Post.new(url.request_uri)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
headers={}
headers["Authorization"]="Zoho-oauthtoken 1000.dfa7XXXXXXXXXXXXXXXXXX84f9665840.c176aeXXXXXXXXXXXX13f3d37a84d"
headers["content-type"]="multipart/form-data"
headers["feature"]="bulk-write"
headers["X-CRM-ORG"]="680100987"
headers&.each { |key, value| req.add_field(key, value) }
file_path = "/Users/test/Desktop/pic.png"
file = File.open(file_path)
data=[["file",file.read, { filename: "pic.png" }]]
req.set_form(data,'multipart/form-data')
response=http.request(req)
status_code = response.code.to_i
headers = response.each_header.to_h
print status_code
print headers
unless response.body.nil?
print response.body
end
end
end
UploadFile.new.execute
Possible Errors
- INVALID_FILE_FORMATHTTP 400
The file you uploaded is not in the zip format.
Resolution: Upload the file only in the zip format. - FILE_TOO_LARGEHTTP 413
The file size is too large to process.
Resolution: The maximum file size is 25MB. - INVALID_URL_PATTERNHTTP 404
Please check if the URL trying to access is a correct one
Resolution: The request URL specified is incorrect. Specify a valid request URL. Refer to request URL section above. - OAUTH_SCOPE_MISMATCHHTTP 401
Unauthorized
Resolution: Client does not have ZohoFiles.files.ALL. Create a new client with valid scope. Refer to scope section above. - NO_PERMISSIONHTTP 403
Permission denied to create
Resolution: The user does not have permission to upload files. Contact your system administrator. - INTERNAL_ERRORHTTP 500
Internal Server Error
Resolution: Unexpected and unhandled exception in Server. Contact support team. - INVALID_REQUEST_METHODHTTP 400
The http request method type is not a valid one
Resolution: You have specified an invalid HTTP method to access the API URL. Specify a valid request method. Refer to endpoints section above. - AUTHORIZATION_FAILEDHTTP 400
User does not have sufficient privilege to create.
Resolution: The user does not have the permission to upload file. Contact your system administrator.
Note
Refer to the Limitations to know the CSV file limits.
Sample Response
Copied{
"status": "success",
"code": "FILE_UPLOAD_SUCCESS",
"message": "file uploaded.",
"details": {
"file_id": "111111000001492610",
"created_time": "2018-12-31T12:00:00-12:00"
}
}