sethuramanraji | Posts: 9

Identifying the MIME Type of the Attachments and Documents

0 votes
Hi All, When I'm consuming the attachment requirements download API and PDF Document download API, It just gives me the stream of data. Is there anyway, that I can get the MIME type of that particular Document/Attachment?. Thanks in Advance.

Duo_Liang | Posts: 3776

Reply to: Identifying the MIME Type of the Attachments and Documents

0 votes
Hi Sethuraman, If you are downloading attachment by GET /api/packages/{packageId}/attachment/{attachmentId}, you will receive the attachment(s) with the original MIME type. The response content-type is application/octet-streamand you just need to store file name with the corresponding extension type or directly open the API url in your browser. Attachment 1 is a screenshot of response OSS will send back to you when you call this API. I also created some snippet for you to download the attachment in REST method in Java in attachment 2. If you are using GET /api/packages/{packageId}/attachment/zip, the MIME type will be application/zip, and when you unzip the file, attachment(s) will be in original type. BTW, the types allowed to upload as an attachment are: PDF, DOC, DOCX, RTF, ODT, JPG, JPEG, PNG, BMP, TXT, XLS, XLSX. And for the documents in your transaction, I think they can only be downloaded with PDF type as all audit trails have to be sealed into the PDF that's why it can't be downloaded as the original type. Even if you are using GET /packages/{packageId}/documents/{documentId}/original, the file will still be in PDF type. Hope this could help you! Duo

Attachments
8-31-1.png29.77 KB
Duo_Liang | Posts: 3776

Reply to: Identifying the MIME Type of the Attachments and Documents

0 votes
And the code snippet here.
public class DownloadAttachment {
	private String API_KEY = "your api key";
	private String url = "https://sandbox.esignlive.com/api/packages/";
	
	// https://sandbox.esignlive.com/a/api/packages/7YuopfUIcfnrP47mBs_M8sHNzrQ=/attachment/2BBmP9p5ctMB
		public void downloadAttachment(String packageId, String docId) throws IOException {
			URL client = new URL(url + packageId + "/attachment/" + docId);
			HttpURLConnection conn = (HttpURLConnection) client.openConnection();
			conn.setRequestProperty("Authorization", "Basic " + API_KEY);

			String contentDisposition = ((HttpURLConnection) conn).getHeaderField("content-disposition");
			String fileName = contentDisposition.substring(contentDisposition.lastIndexOf("filename=\"") + 10,
					contentDisposition.length() - 1);
			InputStream inputStream = conn.getInputStream();
			inputStream = conn.getInputStream();
			String saveFilePath = "C:\\Users\\I5-PC-HDMI\\Documents\\docs\\" + fileName;
			FileOutputStream outputStream = new FileOutputStream(saveFilePath);

			int bytesRead = -1;
			byte[] buffer = new byte[4096];
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, bytesRead);
			}

			outputStream.close();
			inputStream.close();
			conn.disconnect();
		}

		public static void main(String[] args) throws Exception {
			new DownloadAttachment().downloadAttachment("7YuopfUIcfnrP47mBs_M8sHNzrQ=", "2BBmP9p5ctMB");
		}
}

Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off