I’m using JavaScript to select an HDF5 file on the client-side and send it to a Julia server. The Julia server is supposed to save the file and then read data from it. The read data should then be sent back to the web application.
However, I’m encountering an error when trying to save the file on the server. Despite the error, the file is actually being saved correctly.
Furthermore, I have also ruled out any issues with returning the data to the web application, as everything works fine when I exclude the write_h5 function and only read an existing file. This suggests that the problem lies specifically with the write_h5 function or the process of saving the HDF5 file on the server.
I hope i presented my problem clearly.
Thank you in advance for your assistance and support in resolving my issue.
Activating project at `~/julia`
[ Info: Listening on: 127.0.0.1:8081, thread id: 1
┌ Error: handle_connection handler error
│ exception =
│ IOError: write: broken pipe (EPIPE)
│ Stacktrace:
│ [1] uv_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
│ @ Base ./stream.jl:1064
│ [2] unsafe_write(s::Sockets.TCPSocket, p::Ptr{UInt8}, n::UInt64)
│ @ Base ./stream.jl:1118
│ [3] unsafe_write
│ @ ~/.julia/packages/HTTP/NVWp3/src/Connections.jl:122 [inlined]
│ [4] write
│ @ ./strings/io.jl:244 [inlined]
│ [5] write
│ @ ./io.jl:674 [inlined]
│ [6] unsafe_write(http::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.Connections.Connection{Sockets.TCPSocket}}, p::Ptr{UInt8}, n::UInt64)
│ @ HTTP.Streams ~/.julia/packages/HTTP/NVWp3/src/Streams.jl:95
│ [7] write
│ @ ./strings/io.jl:244 [inlined]
│ [8] write(io::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.Connections.Connection{Sockets.TCPSocket}}, s::Base.CodeUnits{UInt8, String})
│ @ Base ./strings/basic.jl:758
│ [9] (::HTTP.Handlers.var"#1#2"{typeof(handle_requests)})(stream::HTTP.Streams.Stream{HTTP.Messages.Request, HTTP.Connections.Connection{Sockets.TCPSocket}})
│ @ HTTP.Handlers ~/.julia/packages/HTTP/NVWp3/src/Handlers.jl:61
│ [10] #invokelatest#2
│ @ ./essentials.jl:729 [inlined]
│ [11] invokelatest
│ @ ./essentials.jl:726 [inlined]
│ [12] handle_connection(f::Function, c::HTTP.Connections.Connection{Sockets.TCPSocket}, listener::HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, readtimeout::Int64, access_log::Nothing)
│ @ HTTP.Servers ~/.julia/packages/HTTP/NVWp3/src/Servers.jl:447
│ [13] (::HTTP.Servers.var"#16#17"{HTTP.Handlers.var"#1#2"{typeof(handle_requests)}, HTTP.Servers.Listener{Nothing, Sockets.TCPServer}, Set{HTTP.Connections.Connection}, Int64, Nothing, Base.S
Julia and package versions:
julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin21.4.0)
CPU: 8 × Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
Threads: 1 on 8 virtual cores
(julia) pkg> st
Status `~/Project.toml`
[f67ccb44] HDF5 v0.16.14
[cd3eb016] HTTP v1.8.1
[682c06a0] JSON v0.21.4
My Julia code:
using HTTP,HDF5,JSON
function handle_requests(req::HTTP.Request)
if startswith(req.target, "/process-file")
return process_file(req)
end
return HTTP.Response(404, CORS_RES_HEADERS, "Not found")
end
HTTP.serve(handle_requests, "127.0.0.1", 8081)
dir = "tmp"
function write_h5(filename,data)
file = joinpath(dir, "$(filename)")
open(file, "w") do fid
write(fid, data)
end
end
function read_h5()
file = readdir(dir,join=true)[1]
fid = HDF5.h5open(file, "r")["Data"]
data = fid["data"] |> read
d = Dict("data" => data)
close(fid)
return d
end
function process_file(req::HTTP.Request)
multipart = HTTP.parse_multipart_form(req)
filename = multipart[1].filename
filedata = multipart[1].data |> read
write_h5(filename,filedata)
d = read_h5()
result = Dict(
"message" => "File saved successfully",
"data" => d
)
return HTTP.Response(200, CORS_RES_HEADERS, JSON.json(result))
end
My HTML code:
<input type="file" id="browse-measurements" accept="application/x-hdf,application/x-hdf5"/>
My JavaScript code:
// Browse on PC
async function sendFileToServer(file) {
const formData = new FormData();
formData.append("file", file);
const response = await fetch("http://127.0.0.1:8081/process-file", {
method: "POST",
body: formData,
header: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":"GET, OPTIONS, HEAD, PUT, POST"},
});
const result = await response.json();
console.log("Ergebnis vom Server:", result.message);
}
document.getElementById("browse-measurements").addEventListener("change", async (event) => {
const file = event.target.files[0];
if (!file) return;
await sendFileToServer(file);
});