ajaxfileupload上传文件

mac2022-06-30  90

Ajax代码:

 

string msg = "";

        string error = "";

        HttpPostedFile ofile = HttpContext.Current.Request.Files[0];//文件对象

        string dat = Path.GetExtension(ofile.FileName).ToLower();

        int size = ofile.ContentLength / 1024;

        string datstr=dat.Remove(0,1);

        int typeid=0;

typeid=Convert.ToInt32( Enum.Parse(typeof(EMB.BLL.Enum.Resource.Layout),datstr));

//判断文件格式是否正确

        if(typeid.Equals(0))

            error="你上传的文件格式不支持!";

        int typeid = SN.Common.Request.GetQueryInt("tp", 0);//url传值

        int classid = SN.Common.Request.GetQueryInt("cid", 0);

        //判断文件夹是否存在

string dirFloder = string.Format(@"/upfiledown/{0}/", SN.Common.Utils.GetUploadFolder());

            if (!Directory.Exists(SN.Common.Utils.GetMapPath(dirFloder)))

            {

                Directory.CreateDirectory(SN.Common.Utils.GetMapPath(dirFloder));

            }

SN.ERP.DAL.UploadFiles obll = new SN.ERP.DAL.UploadFiles();

            int n = obll.Add(omodel);//向数据库添加

            if (n > 0)

            {

                //另存为文件

                string filename = string.Format("{0}{1}", dirFloder, n + dat);

                HttpContext.Current.Request.Files[0].SaveAs(SN.Common.Utils.GetMapPath(filename));

                msg = "数据保存成功!";

            }

            else

                error = "保存失败!";

//向页面返回信息

HttpContext.Current.Response.Write("{msg:'" + msg + "',error:'" + error + "'}");

 

Asp.net 代码

 

  <p>

    <label for="up">上传</label>

    <input type="file" id="upfile" name="upfile" style="cursor:pointer;" />

  </p>

 

 

JQ代码

 

<script type="text/javascript">

$('#okbut').click(function(){

var type=$('#ctl00_ContentPlaceHolder1_ddltype').val();

var classid=$('#ctl00_ContentPlaceHolder1_ddlclass').val();

$.ajaxFileUpload({

//处理页面及传参数 

         url:"/common/ajax/ajax.aspx?"+Math.random()+"&w=updown&tp="+type+"&cid="+classid+,

          secureuri:false,

          fileElementId:"upfile",//上传控件

          dataType:"json",

          success:function(data,status)

            {

                if(typeof(data.error)!='undefined')

                {

                    if(data.error!='')

                     alert(data.error);

                     else

                     alert(data.msg);

                }

            },

            error:function(data,status,e)

            {

                alert(data.error);

            }

      });

    return false;

})

</script>

注意:引用ajaxfileupload.js文件。因为我们使用的ajax上传是别人写的插件!!   <script src="http://www.cnblogs.com/js/ajaxfileupload.js" type="text/javascript"></script>

 

但我不明白为什么数据库添加成功了,,,但页面不能执行提示信息!!??

转载于:https://www.cnblogs.com/sunnysmile/archive/2009/05/13/1456119.html

相关资源:ajaxfileupload多文件上传
最新回复(0)