I try to upload file using MVC4 syntax
@using (Html.BeginForm("ImportCsv","ASPolicy", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
<input type="file" name="file" style="display: none"/>
<input type="text" name="cid" value="'@ViewData["cid"]'" style="display: none"/>
<input type="submit" value="upload" style="display: none"/>
}
and my controller look like
public JsonResult ImportCsv(HttpPostedFileBase file, String cid)
{
IPRestriction ipRestriction = new IPRestriction();
List<string> ipList = new List<string>();
using (BinaryReader b = new BinaryReader(file.InputStream))
{
byte[] binData = b.ReadBytes(Convert.ToInt32(file.InputStream.Length));
string result = System.Text.Encoding.Unicode.GetString(binData);
TextReader textReader = new StringReader(result);
CsvReader csv = new CsvReader(textReader, new CsvConfiguration() {Delimiter = "\t"} );
while (csv.Read())
{
string accountNumber = csv.GetField(0);
string ip = csv.GetField(1);
ipRestriction.accountNumber = accountNumber;
ipList.Add(ip);
}
ipRestriction.ipAllowList = ipList.ToArray();
}
String jsonStr = JsonConvert.SerializeObject(ipRestriction);
return Json(jsonStr, JsonRequestBehavior.AllowGet);
}
This one seen to be not work because all time time submit button is clicked, it will fall to this controller and try to redirect page with json I have return.
So, there is anyway to upload file and get json response, I need to use this json response to render content in this page