You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use react pdf to do the range request, what I am doing is that in the fisrt reqeust, I will response the file size and return the range support headers then the react pdf will auto swich to the range request, but the problem is the fisrt time reqeust will reutn the full pdf file wich more than 500MB. then send the next time range request, after the range request, it will also send another reqeust to fetch the whole file.
is it possible to make the react pdf send range reqeust by default without checking? This is mys server side rust code look like:
pub fn get_partial_pdf(lastest_pdf: &LatestCompile, range: Option<&HeaderValue>) -> HttpResponse {
let proj_base_dir = get_proj_base_dir(&lastest_pdf.project_id);
let pdf_name = format!(
"{}{}",
get_filename_without_ext(&lastest_pdf.file_name),
".pdf"
);
let pdf_file_path = join_paths(&[proj_base_dir, pdf_name]);
if range.is_none() {
let mut file = File::open(pdf_file_path).expect("Failed to open file");
let mut buf = Vec::new();
// file.read_to_end(&mut buf);
let metadata = file.metadata().expect("Failed to get metadata");
let file_size = metadata.len();
return HttpResponse::PartialContent()
.insert_header(CacheControl(vec![CacheDirective::NoCache]))
.append_header(("Accept-Ranges", "bytes"))
.append_header(("Content-Length", file_size))
.append_header((
"Access-Control-Expose-Headers",
"Accept-Ranges,Content-Range",
))
.content_type("application/pdf")
.body(buf);
}
let mut parts = range.unwrap().to_str().unwrap().split('-');
let start = parts.next().unwrap_or("0").parse::<u64>().unwrap_or(0);
let end = parts.next().unwrap_or("0").parse::<u64>().unwrap_or(0);
let mut file = File::open(pdf_file_path).expect("Failed to open file");
let metadata = file.metadata().expect("Failed to get metadata");
let file_size = metadata.len();
file.seek(SeekFrom::Start(start))
.expect("Failed to seek file");
let mut buf = vec![0; (end - start + 1) as usize];
file.take(end - start + 1)
.read_exact(&mut buf)
.expect("Failed to read file");
let content_range = format!("bytes {}-{}/{}", start, end, file_size);
return HttpResponse::PartialContent()
.insert_header(CacheControl(vec![CacheDirective::NoCache]))
.append_header(("Content-Range", content_range))
.append_header(("Accept-Ranges", "bytes"))
.append_header(("Content-Length", file_size))
.append_header((
"Access-Control-Expose-Headers",
"Accept-Ranges,Content-Range",
))
.content_type("application/pdf")
.body(buf);
}
this code will return the whole pdf file size when the header without range, most of the time the fisrt time react pdf request. when handle the range request by range start and end settings.
I was wonder is it possible to let react pdf send range header without download the whole huge pdf.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I want to use react pdf to do the range request, what I am doing is that in the fisrt reqeust, I will response the file size and return the range support headers then the react pdf will auto swich to the range request, but the problem is the fisrt time reqeust will reutn the full pdf file wich more than 500MB. then send the next time range request, after the range request, it will also send another reqeust to fetch the whole file.
is it possible to make the react pdf send range reqeust by default without checking? This is mys server side rust code look like:
this code will return the whole pdf file size when the header without range, most of the time the fisrt time react pdf request. when handle the range request by range start and end settings.
I was wonder is it possible to let react pdf send range header without download the whole huge pdf.
Beta Was this translation helpful? Give feedback.
All reactions