当前位置:编程学习 > C#/ASP.NET >>

asp.net+jquery 文件上传代码(1/2)

asp教程.net+jquery 文件上传代码
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="网页特效">
unction testup()
{
 ajaxfileupload("fileupload1");
}
function ajaxfileupload(obfile_id)
{
//准备提交处理 $("#loading_msg").html("<img src="/images/dotajax.gif" />");
 //开始提交 $.ajax({
  type: "post", url:"ajaxupfile.ashx", data:"upfile="+$("#"+obfile_id).val(),
   success:function
(data, status)
{ //alert(data);
var stringarray = data.split("|"); if(stringarray[0]=="1")
{
    //stringarray[0] 成功状态(1为成功,0为失败) //stringarray[1] 上传成功的文件名 //stringarray[2] 消息提示
    $("#divmsg").html("<img src="/images/note_ok.gif" />"+stringarray[2]+" 文件地址:"+stringarray[1]);
    $("#filepreview").attr({ src:stringarray[1]}); } else { //上传出错 $("#divmsg").html("<img
    src="/images/note_error.gif" />"+stringarray[2]+""); } $("#loading_msg").html("");
},
error:function (data, status, e)
 { alert("上传失败:"+e.tostring()); } });
 return  false;//.net按钮控件取消提交}

</script>
</head>

<body>
asp.net教程 c代码

上传文件 方法
    /// </summary>
    /// <param name="filenamepath"></param>
    /// <returns></returns>
    public string uploadfile(string filenamepath)
    {
        //system.threading.thread.sleep(10000);
        //system.net.mime.mediatypenames.application.doevents();
        return uploadfile(filenamepath, "upload/orgimg/");
    }

    /// <summary>
    /// 上传文件 方法
    /// </summary>
    /// <param name="filenamepath"></param>
    /// <param name="tofilepath"></param>
    /// <returns>返回上传处理结果   格式说明: 0|file.jpg|msg   成功状态|文件名|消息    </returns>
    public string uploadfile(string filenamepath, string tofilepath)
    {
        try
        {
            //获取要保存的文件信息
            fileinfo file = new fileinfo(filenamepath);
            //获得文件扩展名
            string filenameext = file.extension;

            //验证合法的文件
            if (checkfileext(filenameext))
            {
                //生成将要保存的随机文件名
                string filename = getfilename() + filenameext;
                //检查保存的路径 是否有/结尾
                if (tofilepath.endswith("/") == false) tofilepath = tofilepath + "/";
                //if (smallpath.endswith("/") == false) tofilepath = tofilepath + "/";
                //if (waterpath.endswith("/") == false) tofilepath = tofilepath + "/";

                //按日期归类保存
                string datepath = datetime.now.tostring("yyyymm") + "/" + datetime.now.tostring("dd") + "/";
                if (true)
                {
                    tofilepath += datepath;//原图
                    //  smallpath += datepath;//缩略图
                    // waterpath += datepath;//水印图

                }
                //获得要保存的文件路径     --upload/orgimg/2010/07/02/asdafsaf.jpg
                string serverfilename = tofilepath + filename;
                //获得要保存的缩略图文件路径
                // string smallfilename = smallpath + filename;
                //获得要保存的水印图文件路径
                // string waterfilename = waterpath + filename;

                //物理完整路径           --服务器上路径
                string tofilefullpath = httpcontext.current.server.mappath(tofilepath);
                //缩略图物理完整路径
                // string tosmallfullpath = httpcontext.current.server.mappath(smallpath);
                //水印图物理完整路径
                // string toswaterfullpath = httpcontext.current.server.mappath(waterpath);

                //检查是否有该路径  没有就创建
                if (!directory.exists(tofilefullpath))
                {
                    directory.createdirectory(tofilefullpath);
                }

                //检查缩略图是否有该路径  没有就创建
                //if (!directory.exists(tosmallfullpath))
                //{
                //    directory.createdirectory(tosmallfullpath);
                //}

1 2
补充:asp.net教程,C语言 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,