RejectedSoftware Forums

Sign up

Problem with download csv file

Good morning, i need some help, i´m noob for vibe.d. My problem is in download csv file, set headers Content-Type and Content-Disposition, also writing in the body text I needed, but at the time of the post, not download any files. Deputy code:

void postdownload(scope HTTPServerRequest req, scope HTTPServerResponse res)
{

		Json j = ...;
		auto response =  serialize!JsonSerializer(this.makeRequest(anythingUrl, "POST", serialize!JsonSerializer(j)));
		res.headers["Content-Disposition"] = "attachment; filename=Reporte.txt";
		res.headers["Cache-Control"] = "no-cache";
		//res.headers["Expires"] = "-1";
		res.headers["Content-Description"] = "File Transfer";
		//res.headers["Content-Encoding"] = "UTF-8";
		//res.headers["Pragma"] = "no-cache";
		string csvFile = response.rbody.toString();
		res.writeBody(csvFile,"text/csv");
	
}

Re: Problem with download csv file

On Wed, 11 May 2016 06:30:16 GMT, mauricio wrote:

Good morning, i need some help, i�m noob for vibe.d. My problem is in download csv file, set headers Content-Type and Content-Disposition, also writing in the body text I needed, but at the time of the post, not download any files. Deputy code:

void postdownload(scope HTTPServerRequest req, scope HTTPServerResponse res)
{

Json j = ...;
auto response =  serialize!JsonSerializer(this.makeRequest(anythingUrl, "POST", serialize!JsonSerializer(j)));
res.headers["Content-Disposition"] = "attachment; filename=Reporte.txt";
res.headers["Cache-Control"] = "no-cache";
//res.headers["Expires"] = "-1";
res.headers["Content-Description"] = "File Transfer";
//res.headers["Content-Encoding"] = "UTF-8";
//res.headers["Pragma"] = "no-cache";
string csvFile = response.rbody.toString();
res.writeBody(csvFile,"text/csv");

}

This generally looks right. As a side note, I'd just recommend to serialize to a JSON string directly for efficiency reasons:

auto response = this.makeRequest(anythingUrl, "POST", serializeToJson(j));
...
res.writeBody(serializeToJsonString(response.rbody), "text/csv");
// or, using chunked transfer, but avoiding the dynamic memory allocation:
// auto dst = StreamOutputRange(res.bodyWriter);
// dst.serializeToJson(response.rbody);

But regarding the actual problem, which client do you use for downloading? What are the exact symptoms? Does the request stall, or does it download an empty file?

Re: Problem with download csv file

On Tue, 17 May 2016 06:52:30 GMT, Sönke Ludwig wrote:

This generally looks right. As a side note, I'd just recommend to serialize > to a JSON string directly for efficiency reasons:

Wait, there is actually res.writeJsonBody(response.rbody), which does the right thing and never allocates. It also has an allow_chunked switch to control if the "Content-Length" header is always used, or if chunked transfer is allowed.