I am uploading Excel file and i want when i click on upload button if its Excel file its show the msg "File Uploaded Successfully" otherwise its incorrect file i.e img. doc etc its show the msg "you must be select the Excel Format file" kindly help how i do this on AJAX.
This is my Controller its uploading sucessfully.
//Post
[HttpPost, ActionName("Email_Load")]
public ActionResult Email_Load_confirmed(HttpPostedFileBase file)
{
File_Model tz = new File_Model();
try
{
if (file != null)
{
file.SaveAs(Server.MapPath("~/App_Data/Uploads/Contacts.xls"));
_Stores Stores_ = new _Stores();
Basic_Helper _Basic_Helper = new Basic_Helper();
var Store = Stores_.Get_Store_Info_Prd(_Basic_Helper.Format_URL(Request.Url.Host.ToString()));
if (Store != null)
{
_Basic_Helper = null;
_Site_Info Site_Info = new _Site_Info();
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/Uploads/Contacts.xls") + ";";
connectionString += "Extended Properties=Excel 8.0;";
OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];");
OleDbConnection myConnection = new OleDbConnection(connectionString);
myConnection.Open();
myCommand.Connection = myConnection;
OleDbDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if (!myReader.GetValue(1).ToString().Equals(""))
{
Site_Info.Insert_Cust_Det_Newsletter(Store.ID, myReader.GetValue(0).ToString(), myReader.GetValue(1).ToString());
}
}
myReader.Close();
myCommand.Cancel();
myConnection.Close();
myConnection = null;
Site_Info = null;
}
Store = null;
Stores_ = null;
System.IO.File.Delete(Server.MapPath("~/App_Data/Uploads/Contacts.xls"));
}
}
catch (Exception ex)
{
string s = ex.ToString();
s = "";
}
return View();
}
its Razor View
@using (Html.BeginForm("Email_Load", "Marketing", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table class="a_m_s">
<tr>
<td>File: </td>
<td>
<input type="file" name="file" id="file" data-show-preview="false" style="border: 4px solid rgb(224, 0, 0);
padding: 1px;
color: rgb(255, 255, 255);
background: rgb(85, 85, 85) none repeat scroll 0% 0%; clear:both; margin-top:10px;"></td>
</tr>
<tr>
<td></td>
<td> <br />
<input type="submit" id="load" value="Upload File" style=" background-color: #e00000; clear: both; color: #fff; border: 3px solid #e00000; margin-left: 10px;"/></td>
</tr>
</table>
}
Then How i do on AJax???
Thanks Regards,
Tahir