zoukankan      html  css  js  c++  java
  • JQuery上传图片实例

    <divstyle="display:none"id="upimg"><tablecellpadding="0"cellspacing="0"style="border-collapse:collapse;border:solid 1px#9bc4e9;width:360px">
           
    <tr>
               
    <tdalign='center'style='width:100px;border:solid 1px#9bc4e9'>图片:</td>
               
    <tdstyle='border:solid 1px#9bc4e9;height:35px'align='center'>
               
    <inputid='imgfile'type='file'/>
                保存ID的
    <inputid="Hidden1"type="hidden"/>
                保存File控件值的
    <inputid="Hidden2"type="hidden"/>
               
               
    </td>
           
    </tr>
           
    <tr>
               
    <tdalign='center'style='width:100px;border:solid 1px#9bc4e9;height:35px'></td>
               
    <tdstyle='border:solid 1px#9bc4e9'align='center'><inputid="Button1"type="button"value="上传"/></td>
           
    </tr>
       
    </div>

    JS文件:

    $(function(){
        $
    ("#Button1").click(function(){
           
    var path = $("#Hidden1").val();
           
    if(path.length==0)
           
    {
                alert
    ("请上传图片附件");
               
    return;
           
    }
            $
    .post("../ashx/Article.ashx",{id:$("#Hidden2").val(),file:path},function(data){
               
    if(parseInt(data)>0)
               
    {
                    alert
    ("上传成功");
                   
    return;
               
    }
               
    else
               
    {
                    alert
    ("上传失败");
                   
    return;
               
    }
           
    })
       
    })
       
        $
    ("#imgfile").change(function(){
            $
    ("#Hidden1").val($(this).val());
       
    })
    })

    ashx代码:

    <%@WebHandlerLanguage="C#"Class="Article"%>

    using
    System;
    using
    System.Web;
    using
    Web.Dal;
    using
    System.Web.UI;
    using
    System.Web.UI.HtmlControls;
    using
    System.Web.UI.WebControls;
    using
    System.Web.UI.WebControls.WebParts;
    using
    System.IO;
    using
    System.Net;

    publicclassArticle:IHttpHandler{
       
       
    publicvoidProcessRequest(HttpContext context){
            context
    .Response.ContentType="text/plain";
           
    string path = context.Request.Form["file"];
           
    int id =Convert.ToInt32(context.Request.Form["id"]);
           
    //FileUpload fu = new FileUpload();
           
    //fu.PostedFile.FileName = path;
           
    string savepath =string.Format("WebUI/File/NewsImg/{0}-{1}/",DateTime.Now.Year,DateTime.Now.Month);
           
    if(!Directory.Exists(context.Server.MapPath("~/"+ savepath)))
               
    Directory.CreateDirectory(context.Server.MapPath("~/"+ savepath));
           
    string sty = path.Substring(path.LastIndexOf('.'));
            savepath
    +=DateTime.Now.Ticks.ToString()+ sty;
           
    WebClient myWebClient =newWebClient();
           
    //设定window网络安全认证
            myWebClient
    .Credentials=CredentialCache.DefaultCredentials;

           
    //要上传的文件
           
    FileStream fs =newFileStream(path,FileMode.Open,FileAccess.Read);

           
    BinaryReader br =newBinaryReader(fs);
           
    //使用UploadFile方法可以用下面的格式
            myWebClient
    .UploadFile(savepath, path);

            context
    .Response.Write(newArticleServer().EditImg(id, savepath));
       
    }
     
       
    public bool IsReusable{
            get
    {
               
    returnfalse;
           
    }
       
    }

    }
  • 相关阅读:
    共用体
    位运算符和位运算
    python中while循环
    c语言中求多个整数的和及其平均值
    c语言中switch语句
    python中删除列表中多次重复的元素
    python中while循环
    python中break语句
    使用用户输入来填充字典
    python中while循环使用标志
  • 原文地址:https://www.cnblogs.com/zcttxs/p/2651268.html
Copyright © 2011-2022 走看看