您的位置:首页 > 其它

ajaxfileupload上传文件成功,但是无法获取到值

2015-10-26 11:31 435 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/shanfaxiang/article/details/49421837 <script src="~/Scripts/jquery-1.7.1.js"></script>
    <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);
            }
        }



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐