File Erlang:发送文件和文件名

File Erlang:发送文件和文件名,file,sockets,erlang,gen-tcp,File,Sockets,Erlang,Gen Tcp,我很想发送一个文件和它的文件名。 服务器选项: -define(TCP_OPTIONS_SERVER, [binary, {packet, 0}, {active, false}]). 这就是接收器循环: file_receiver_loop(Socket,Bs)-> case gen_tcp:recv(Socket, 0) of {ok, B} -> file_receiver_loop(Socket, [Bs, B]); {error, clos

我很想发送一个文件和它的文件名。 服务器选项:

-define(TCP_OPTIONS_SERVER, [binary, {packet, 0}, {active, false}]).
这就是接收器循环:

file_receiver_loop(Socket,Bs)->
case gen_tcp:recv(Socket, 0) of
    {ok, B} ->
        file_receiver_loop(Socket, [Bs, B]);
    {error, closed} ->
        io:format("~nReceived!~n ~p",[Bs]),
        save_file(Bs)
end.
我发送文件时附带:

file:sendfile(FilePath, Socket),
当我发送文件和文件名时

gen_tcp:send(Socket,Filename),
file:sendfile(FilePath, Socket),
二进制数据具有可变结构。


谢谢大家

我编写这段代码是为了解决这个问题。
我发送30字节作为文件名。

如果文件名我做了两个函数,你可以通过消息传递发送二进制文件

%Zip a file or a folder
send_zip(Pid, FolderName) ->
    %Name of you zip file
    FolderNameZip= FolderName++".zip",   
    %Zip the folder/file from the computer and name the zip file by FornderNameZip                    
    {ok, ZipFile} = zip:create(FolderNameZip, [FolderName]),
    %Put the Zipfile in a variable 
    {ok, ZipHandle} = file:read_file(ZipFile),
    %Send the zipfile to the other PID, sends the zipfile and his name to name it the same way.
    Pid ! {finish, ZipHandle, FolderNameZip}.


%save and unzip the in the computer 
register_zip_bash(Dir, ZipHandle, FolderNameZip) -> 
    file:write_file(FolderNameZip, ZipHandle),
    Cmd2 = io_lib:format("unzip -o -j ~p -d ~p/",[FolderNameZip, Dir]),
    _=os:cmd(Cmd2).
FoldName可以是您的文件名或计算机中的文件夹名。 Pid是您想要发送消息的人

当您收到ZipHandle和FolderNameZip时,请使用register_zip_bash(…)函数将数据保存到计算机中。 Dir是要保存数据的目录。 ZipHandle是从PID函数接收的二进制文件。 FolderNameZip是将ZipHandle保存在计算机中的位置的名称

如您所见,send_-zip(…)使用erlang函数,register_-zip_-bash(…)使用bash命令。发送数据不需要压缩文件。请毫不犹豫地更改代码,使其适合您

希望这会有所帮助,
干杯。

有新的方法吗?
file_receiver_loop(Socket,Filename,Bs)->
    io:format("~nRicezione file in corso~n"),
    case gen_tcp:recv(Socket, 0) of
    {ok, B} ->
        file_receiver_loop(Socket, Filename,[Bs, B]);
    {error, closed} ->
        save_file(Filename,Bs)
end.
%%salva il file
save_file(Filename,Bs) ->
    io:format("~nFilename: ~p",[Filename]),
    {ok, Fd} = file:open("../script/"++Filename, write),
    file:write(Fd, Bs),
    file:close(Fd).
%%Permette l'invio di un file specificando host,filename e path assoluto
send_file(Host,Filename,FilePath,Port)->
    {ok, Socket} = gen_tcp:connect(list_to_atom(Hostname), Port,          TCP_OPTIONS_CLIENT),
    FilenamePadding = string:left(Filename, 30, $ ), %%Padding with white space
    gen_tcp:send(Socket,FilenamePadding),
    Ret=file:sendfile(FilePath, Socket),
    ok = gen_tcp:close(Socket).
%Zip a file or a folder
send_zip(Pid, FolderName) ->
    %Name of you zip file
    FolderNameZip= FolderName++".zip",   
    %Zip the folder/file from the computer and name the zip file by FornderNameZip                    
    {ok, ZipFile} = zip:create(FolderNameZip, [FolderName]),
    %Put the Zipfile in a variable 
    {ok, ZipHandle} = file:read_file(ZipFile),
    %Send the zipfile to the other PID, sends the zipfile and his name to name it the same way.
    Pid ! {finish, ZipHandle, FolderNameZip}.


%save and unzip the in the computer 
register_zip_bash(Dir, ZipHandle, FolderNameZip) -> 
    file:write_file(FolderNameZip, ZipHandle),
    Cmd2 = io_lib:format("unzip -o -j ~p -d ~p/",[FolderNameZip, Dir]),
    _=os:cmd(Cmd2).