我如何验证正确的格式文件 [英] How I Do Validate The Correct Format File

查看:79
本文介绍了我如何验证正确的格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上传Excel文件,我希望当我点击上传按钮时,如果它的Excel文件显示msgFile Uploaded Successfully,否则它的错误文件即img。 doc等它显示msg你必须选择Excel格式文件,请帮助我在AJAX上做这个。

这是我的控制器上传成功。

/ / post

 [HttpPost,ActionName(  Email_Load )] 
public ActionResult Email_Load_confirmed(HttpPostedFileBase file)
{
File_Model tz = new File_Model();

尝试
{
如果(文件! = null
{
file.SaveAs(Server.MapPath( < span class =code-string>〜/ 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;数据源= + Server.MapPath( 〜/ App_Data / Uploads / Contacts.xls)+ ;;
connectionString + = 扩展属性= Excel 8.0;;
// 始终从sheet1中读取。
OleDbCommand myCommand = new OleDbCommand( 从[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 (例外情况)
{
string s = ex.ToString();
s = ;
// Response.Write(ex.ToString());
}
return View();
}



Razor View

 @ using(Html.BeginForm(Email_Load, Marketing,FormMethod.Post,new {enctype =multipart / form-data}))
{

< class = a_m_s >
< tr >
< td > 文件:< / td >
< span class =code-keyword>< td >
< 输入 type = file 名称 = 文件 id = 文件 data-show-preview = false style = border:4px solid rgb(224,0,0);
填充:1px;
color:rgb(255,255,255);
背景:rgb(85,85,85)无重复滚动0%0%;明确:两者; margin-top:10px;
> < / td >
< / tr >
< tr >
< td > < / td >
< td > < br / >
< input type = submit id = load value = 上传文件 style = background-color:#e00000;明确:两者;颜色:#fff; border:3px solid#e00000; margin-left:10px; / > < / td >
< / tr >
< / table >

}



然后我怎么做AJax ???

谢谢你,

Tahir

解决方案

;);
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 (例外情况)
{
string s = ex.ToString();
s = ;
// Response.Write(ex.ToString());
}
return View();
}



Razor View

 @ using(Html.BeginForm(Email_Load, Marketing,FormMethod.Post,new {enctype =multipart / form-data}))
{

< class = a_m_s >
< tr >
< td > 文件:< / td >
< span class =code-keyword>< td >
< 输入 type = file 名称 = 文件 id = 文件 data-show-preview = false style = border:4px solid rgb(224,0,0);
填充:1px;
color:rgb(255,255,255);
背景:rgb(85,85,85)无重复滚动0%0%;明确:两者; margin-top:10px;
> < / td >
< / tr >
< tr >
< td > < / td >
< td > < br / >
< input type = submit id = load value = 上传文件 style = background-color:#e00000;明确:两者;颜色:#fff; border:3px solid#e00000; margin-left:10px; / > < / td >
< / tr >
< / table >

}



然后我怎么做AJax ???

谢谢你,

Tahir


如果浏览器支持html5的fileapi,你可以在文件上传之前检查扩展名



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



如果你想得到非常技术你可能会把文件的前几个字节作为blob读取并检查excel格式的幻数



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



如果您在上传之前谷歌检查文件扩展名你'我可能会找到一些更具体的例子。如果客户端不支持html5,那么这是不可能的,您需要使用现有代码。


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;";
                        // 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

@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

解决方案

;"); 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

@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


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.


这篇关于我如何验证正确的格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆