2012-08-16 107 views
0

我想通過elisp中的XML-RPC從文件(PNG圖像)傳輸一個二進制數據的blob。這是自動將附件上傳到Confluence wiki的一部分,特別是當前頁面中使用的本地圖像。 這樣的代碼:在elisp中讀取二進制數據

 ;; Open the file and get content 
     (with-temp-buffer 
      ;; file-name is the PNG file name, which is binary data 
      (find-file (expand-file-name file-name current-dir)) 
      ;; Setup Confluence request alist 
      (setq confl-req (list 
          (cons "fileName" file-name) 
          (cons "contentType" mime-type))) 
      ;; RPC call 
      (setq confl-reply (cfln-rpc-execute 'confluence1.addAttachment 
               page-id confl-req (buffer-substring-no-properties (point-min) (point-max)))) 

我有一塊(buffer-substring-no-properties (point-min) (point-max))一個問題。上傳到Confluence的二進制數據與幾個位置的PNG文件不匹配。我注意到附件中的0xe0 0x88字節被替換爲0xc8。任何想法如何獲得包含在文件中的確切的二進制數據?

感謝, NMA

回答

3

您應該使用insert-file-contents-literally而不是find-file

(insert-file-contents-literally FILENAME &optional VISIT BEG END 
REPLACE) 

Like `insert-file-contents', but only reads in the file literally. 
A buffer may be modified in several ways after reading into the buffer, 
to Emacs features such as format decoding, character code 
conversion, `find-file-hook', automatic uncompression, etc. 

This function ensures that none of these modifications will take place. 
+0

這工作得很好!在此之後我可以使用(緩衝區字符串)嗎?我不希望文本屬性被返回。 – 2012-08-17 04:57:13

+0

@NarendraAcharya可能沒有文本屬性,但使用'buffer-substring-no-properties'更安全 – npostavs 2012-08-17 15:57:33

0

即進入在@npostavs答案的線一站式溶液被F庫提供: https://github.com/rejeep/f.el

f-read-bytes 

它使用與

insert-file-contents-literally 

一起

buffer-substring-no-properties 

負責編碼和多字節處理....