<script src="~/Scripts/json2.js"></script>
<script src="~/Scripts/ajaxfileupload.js"></script>
<script>
<div style="padding-left: 80px;">
<input id="fileUpload1" name="fileUpload1" type="file" />
<br />
<br />
name值必须有的!
<br />
<br />
<input type="button" id="btnReader" name="btnReader" value="确定" style="width: 80px; height: 35px" οnclick="reader()" />
</div>
function reader()
{
$.ajaxFileUpload({
url: '../TxtReader/Reader',
// cache: false,
// async: false,
fileElementId: 'fileUpload1',
dataType: 'json',
success: function (data, status) {
alert("Success");
} ,
error: function (data, status, e) {
alert(e);
}
});
}
</script>
/// <summary>
/// 读取文件
/// </summary>
/// <returns></returns>
public string Reader()
{
HttpFileCollectionBase files = Request.Files;
HttpPostedFileBase file = files["fileUpload1"];
string filepath;
string filename;
if (file == null || file.ContentLength <= 0)
{
return "0"; //没有选择需要的上传文件
}
try
{
filepath = GetMapPath("../ExcelFolder/");
filename = Guid.NewGuid().ToString() + file.FileName.Substring(file.FileName.IndexOf('.'));
file.SaveAs(Path.Combine(filepath + filename));
}
catch (Exception err)
{
}
return "0";
}
/// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">指定的路径</param>
/// <returns>绝对路径</returns>
public string GetMapPath(string strPath)
{
if (HttpContext.CurrentHandler != null)
{
return HttpContext.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("~"))
{
strPath = strPath.TrimStart('~');
}
if (strPath.StartsWith("\\"))
{
strPath = strPath.TrimStart('\\');
}
if (strPath.Substring(0, 4) == "bin\\")
{
strPath = strPath.Substring(4);
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}