问题描述
除了我们使用以下代码生成XML:
Further to my old question, We are generating XML using following code:
download_xml('GET', [])->
Xml =generateXML(123445),
%% generated Xml data in string without any values 400,.etc
Filename = export_xml:get_file_name(?SESSION_ID1, ?SESSION_ID2),
Filepath = "./priv/static/" ++ Filename,
TotalSize = filelib:file_size(Filepath),
{ok, FP} = file:open(Filepath, [read]),
Generator = fun(FH) ->
case file:read(FH, 1024) of %% But this line is causing something that we never wanted.
eof -> file:close(FH),
done;
{ok, Data} ->
{output, Data, FH}
end
end,
{stream, Generator, FP, [
{"Content-Type", "application/force-download"},
{"Content-Disposition", "attachment; filename=" ++ Filename},
{"Content-length", TotalSize}
]}.
我们正在使用文件读取块中的文件:read(FH,1024)
。但是,这一行还附加了一些数字 400
, 400
, 3b2
在每个块。我们已经观察到,这些代码只是每个块的十六进制值。
以下是示例XML:
We are reading files in chunks using file:read(FH, 1024)
by this line. But this line is also appending some numbers 400
, 400
, 3b2
in each chunk. We have observed that those codes are nothing but the Hexadecimal values for each chunk. Here is the sample XML :
sample.xml
400
<?xml version="1.0" encoding="UTF-8"?>.....</info><inf
400
tel>4444</tel>...<address></address>
3b2
<name> Abc</name><surname>EFg</surname><city>XYZ</city>....
</DATA>
0
由于将块大小更改为 2048
从 1024
(即文件:读(FH,2048)
)值也被更改为 808
, 365
, 0
。
Since, on changing the chunk size to 2048
from 1024
(i.e file:read(FH, 2048)
) values also get changed to 808
, 365
, 0
.
我们不了解的是:
- 在以块为单位流传输文件内容时,每个块都附加,它的(chunk)尺寸在XML中,然后插入实际块
What we're not understanding is:- While streaming the file contents in chunks, each chunk is appending, it's (chunk's) size in the XML and then actual chunk is inserted.
这里是要生成的小型XML( 93 Bytes
):
Here is small XML wanted to generate has size (93 Bytes
):
<?xml version="1.0">
<info>
<name> Abc</name>
<surname>EFg</surname>
<city>XYZ</city>
</info>
生成后,输出为:
5d
<?xml version="1.0">
<info>
<name> Abc</name>
<surname>EFg</surname>
<city>XYZ</city>
</info>
0
5d = 93(Chunk size)
在这种情况下文件大小。
5d = 93 (Chunk size)
In this case file size.
问题是:
- 为什么大块在每个块之间追加大小,同时使用Generator流式传输文件?
注意 - 我们也尝试从代码中删除标题列表 {Content-length,TotalSize}
,但没有工作:(
NOTE - We also tried removing header list {"Content-length", TotalSize}
from the code, but did not work :(
推荐答案
我在erlang bug邮件列表中看到一个与你的问题有关的交换:
I saw an exchange in the erlang bug mailing list which looks related to your problem : Misleading docs or implementation of file:read/2 and friends
似乎使用文件:读/ 2不是100%清洁与 utf8 选项,他们建议使用io:读/ 3,但我看看如何处理大块和潜在的新行。
It seems that the usage of file:read/2 is not 100% clean with the utf8 option. They recommend to use instead io:read/3, but I don't see how to deal with chunks and potential new line.
这篇关于为什么大块在Erlang / ChicagoBoss中读取文件时返回一些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!