I want to be able to download a csv file on click on a button.
Below is my jquery call to the api 'exportfruit'
function downloadFile(){
var data = {
StartDate: this.model.get('StartDate'),
Name: this.model.get('Name')
};
var form = document.createElement('form');
form.action = 'api/fruitapi/exportFruit';
form.method = 'POST';
form.style.display = 'none';
for (i in data) {
if (data[i] != "") {
var inputElement = document.createElement('textarea');
inputElement.name = i;
inputElement.value = data[i];
form.appendChild(inputElement);
}
}
document.body.appendChild(form);
form.submit();
}
and my web api action is as below
[ActionName("ExportFruit")]
public HttpResponseMessage PostExportFruit(SomeModel model)
{
// for now i am just testing the value returned from model.
string csv = "some data from db";
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StringContent(csv);
//a text file is actually an octet-stream (pdf, etc)
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
//we used attachment to force download
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "testfile.csv";
return result;
}
now the problem I am facing is that I am passing date as 'dd/mm/yyyy' but in the web api action it converts date into 'mm/dd/yyyy'
So for example,
if I have a date like
1/2/2012 this is converted to 2/1/2012
if 22/10/2012 (todays date) is converted to 01/01/0001
How do I fix this ?
I had similar problem when passing json data which I fixed by using this But I have no idea on how to go about on this one.
Please help me on this, as there is hardly any content available on internet for this.
How you define date proprety in SomeModel is it string or Datetime.
Sorry I didn't get your problem exactly do you have problem with searilization(Posting Data to service) or deserialization(Getting data from service) .
yrshaikh
Member
3 Points
8 Posts
Date format changes to 'mm/dd/yyyy' when posting data to web api method
Oct 22, 2012 07:31 AM|LINK
I am using ASP.NET MVC 4 with Web API.
I want to be able to download a csv file on click on a button.
Below is my jquery call to the api 'exportfruit'
function downloadFile(){ var data = { StartDate: this.model.get('StartDate'), Name: this.model.get('Name') }; var form = document.createElement('form'); form.action = 'api/fruitapi/exportFruit'; form.method = 'POST'; form.style.display = 'none'; for (i in data) { if (data[i] != "") { var inputElement = document.createElement('textarea'); inputElement.name = i; inputElement.value = data[i]; form.appendChild(inputElement); } } document.body.appendChild(form); form.submit(); }and my web api action is as below
[ActionName("ExportFruit")] public HttpResponseMessage PostExportFruit(SomeModel model) { // for now i am just testing the value returned from model. string csv = "some data from db"; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StringContent(csv); //a text file is actually an octet-stream (pdf, etc) result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); //we used attachment to force download result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); result.Content.Headers.ContentDisposition.FileName = "testfile.csv"; return result; }now the problem I am facing is that I am passing date as 'dd/mm/yyyy' but in the web api action it converts date into 'mm/dd/yyyy'
So for example,
if I have a date like
How do I fix this ?
I had similar problem when passing json data which I fixed by using this But I have no idea on how to go about on this one.
Please help me on this, as there is hardly any content available on internet for this.
dhanekula
Member
101 Points
37 Posts
Re: Date format changes to 'mm/dd/yyyy' when posting data to web api method
Nov 06, 2012 07:36 PM|LINK
Hi,
How you define date proprety in SomeModel is it string or Datetime.
Sorry I didn't get your problem exactly do you have problem with searilization(Posting Data to service) or deserialization(Getting data from service) .