Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
[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;";
                        // always read from the sheet1.
                        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 = "";
               // Response.Write(ex.ToString());
            }
 return View();
        }

its Razor View
HTML
@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
Posted
Updated 9-Sep-15 0:28am
v2
Comments
[no name] 9-Sep-15 6:48am    
Add accept attribute to input control.
<input type="file" name="file" id="file" data-show-preview="false" accept=".xls,.xlsx" />

1 solution

If the browser supports html5's fileapi you can check the extension before the file is uploaded

http://www.html5rocks.com/en/tutorials/file/dndfiles/[^]

If you wanted to get really technical you could probably read the first few bytes of the file as a blob and check for the excel format's "magic number"

http://www.garykessler.net/library/file_sigs.html[^]

If you google for checking file extension before upload you'll probably find some more specific examples. If the client doesn't support html5 then this isn't possible, you'll need to use your existing code.
 
Share this answer
 

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