Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I am working on a task in that I want to import .bat file.
And want to show .bat file content in multi-line textbox. User can edit that
file content and user can again export that content as .bat file. For that I am using
ASP.NET MVC,JQUERY. I have succeeded to import content to textbox but am not able to
export that content as .bat file(not able to create save file dialog-box popup in asp.net mvc).
Please suggest me for this task.

I have two buttons(Import and Export) to submit same form so I have differentiate them using their name and value.

My code is as follow:

C#
[HttpPost]
        public ActionResult BatchFileSettings(string ImportExport, HttpPostedFileBase myfile, FormCollection formCollection)
        {
            if (ImportExport == "Import")
            {
                string result = "";
                if (myfile != null)
                {
                    BinaryReader b = new BinaryReader(myfile.InputStream);
                    byte[] binData = b.ReadBytes(Convert.ToInt32(myfile.InputStream.Length));
                    result = System.Text.Encoding.UTF8.GetString(binData);
                }

                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else if (ImportExport == "Export")
            {
                string content = formCollection["BatchFile.Content"].ToString();
                return File(Encoding.ASCII.GetBytes(content), "application/octetstream", "DFBatchFile.bat");
            }
            else
            {
                return Json("");
            }
        }


Thanks
Posted
Updated 25-Jun-13 1:26am
v2
Comments
Zoltán Zörgő 25-Jun-13 7:47am    
You should not mix these functions in a single action. It is obviously misleading you.
Maddy_1008 25-Jun-13 13:12pm    
Thank for reply.

Then please tell me other way to do this task.
Maddy_1008 26-Jun-13 5:45am    
Hello All

If I call like this then its working fine.

//HTML button
Export

//JQUERY
$('#aExport').click(function () {
$(this).attr("href", "/Policy/DownloadBatchFile?fileContent=" + $("#BatchFile_Content").val());
});


//Controller Method
[HttpGet]
public ActionResult DownloadBatchFile(string fileContent)
{
return File(Encoding.ASCII.GetBytes(fileContent), "content-disposition", "DFBatchFile.bat");
}

But I dont want to send data from querystring like this. So please help me to solve this problem.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900