0

I'm kind of newbie in C#.net . Im doing a form post on Client side which post a csv to my Controller

in My controller I'm unable to read the value. My goal is to finally create a csv file hence I'm avoiding the ajax post. I have the action on my form directing to the correct Url and Im appending the input to the form.

I have something like this on my client side

    WatchList_Class.prototype.Downloads1 = function(csv) {
 var input = '<input type= "text" id="uploadCsv" name="uploadCsv" value=""' +        csv + ' />';
     $('form#uploadCSV').append(input);
     $('form#uploadCSV').submit();
    };

On the controller side I have

   public ActionResult ExportCSVFile(string data) //Im getting data as null
{
  string toReturn = Server.UrlDecode(data);
  return File(System.Text.Encoding.UTF8.GetBytes(toReturn), "text/csv", "exportedData.csv");

}  

Im getting string data as null;

Can Any one point out what Im doing wrong. Thanks in advance

1
  • Well, the non-nullness of data will be entirely predicated on your having a form element with a name of data somewhere on the client. Nothing in the code you provided indicates one way or another that you have such an element. Commented Mar 7, 2012 at 22:27

4 Answers 4

3

Try with this action method:

[HttpPost]
public ActionResult ExportCSVFile(HttpPostedFileBase uploadCsv)
{
    uploadCsv.SaveAs("exportedData.csv");
}

(Also note that in your code, the name of your paramater data did NOT match the name of your input control uploadCsv)

Sign up to request clarification or add additional context in comments.

2 Comments

I have a quick question on the same line what would be the Controller code will be like for reading a string from a form post say the string contain a password. Thanks in Advance
Doesn't matter if it's a password, it's still a string on the server side. So just use ... MyAction(string password) { ... }
1

Your parameter has to match the name of the POST variable. So instead of data use uploadCsv:

[HttpPost]
public ActionResult ExportCSVFile(string uploadCsv) 
{
  string toReturn = Server.UrlDecode(data);
  return File(System.Text.Encoding.UTF8.GetBytes(toReturn), "text/csv", "exportedData.csv");
}

Comments

0

Have you set the encoding type on the upload form?

enctype="multipart/form-data"

Comments

0

Most likely reason is your ID for data does not match name of the argument in your ExportCSVFile handler. Use Fiddler to check what is actually posted to your page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.