我正在上传Excel文件,我希望当我点击上传按钮时,如果它的Excel文件显示了msg" File Uploaded Sucessfully"否则是不正确的文件,即img。 doc等它显示msg"你必须选择Excel格式文件"请帮助我在AJAX上做到这一点。
这是我的控制器上传成功。
//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();
}

它的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>
}
&#13;
那我怎么做AJax ???
谢谢你,
塔希尔
答案 0 :(得分:0)
以下是通过Ajax上传文件的示例,其中包括对Excel文件的检查。这在IE9或更低版本中无效。如果您需要使用旧版浏览器上传,我建议使用以下插件:
https://github.com/blueimp/jQuery-File-Upload
http://plugins.krajee.com/file-input
<强>客户端/浏览器:强>
<script type="text/javascript">
$(function() {
var fileUpload = function() {
var file = document.getElementById("file").files[0];
var xhr = new window.XMLHttpRequest();
xhr.onload = function () {
if (this.status === 200) {
var data = JSON.parse(this.responseText);
// do stuff...
} else {
alert("Upload Failed. Your request was not completed successfully; reason: " + this.statusText);
}
};
xhr.onerror = function () {
alert("A network related error occurred when trying to process this request. Please check your connection and try again.");
};
xhr.open("POST", "/Home/UploadExcel", true);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(file);
};
$("#load").on("click", function () {
var file = document.getElementById("file").files[0];
var fileName = file.name;
var fileExtension = fileName.substr((fileName.lastIndexOf('.') + 1));
if (fileExtension === "xls" || fileExtension === "xlsx") {
fileUpload();
} else {
alert("Must be Excel file");
}
});
});
</script>
<强>控制器:强>
[HttpPost]
public ActionResult UploadExcel()
{
var error = true;
var errorMessage = String.Empty;
try
{
if (Request.InputStream != null)
{
//do stuff...
error = false;
}
else
{
errorMessage = "File was null";
}
}
catch (Exception x)
{
errorMessage = "Problem in uploading excel file; reason: " + x.Message;
}
var dto = new
{
error,
errorMessage
};
return Json(dto);
}