zoukankan      html  css  js  c++  java
  • Jquery异步上传图片

    网页中这样:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1">
    </head>
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
    function uploadImage() {
    //判断是否有选择上传文件
    var imgPath = $("#uploadFile").val();
    if (imgPath == "") {
    alert("请选择上传图片!");
    return;
    }
    //判断上传文件的后缀名
    var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
    if (strExtension != 'jpg' && strExtension != 'gif'
    && strExtension != 'png' && strExtension != 'bmp') {
    alert("请选择图片文件");
    return;
    }
    $.ajax({
    type: "POST",
    url: "handler/UploadImageHandler.ashx",
    data: { imgPath: $("#uploadFile").val() },
    cache: false,
    success: function (data) {
    alert("上传成功");
    $("#imgDiv").empty();
    $("#imgDiv").html(data);
    $("#imgDiv").show();
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
    alert("上传失败,请检查网络后重试");
    }
    });
    }
    </script>

    <body>
    <form enctype="multipart/form-data" method="post">
    <input type="file" id="uploadFile" />
    <input type="button" id="btnUpload" value="确定" onclick="uploadImage()" />
    <div id="imgDiv">
    </div>
    </form>
    </body>
    </html>

    一般处理程序中这样:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;

    namespace test
    {
    /// <summary>
    /// UploadImageHandler 的摘要说明
    /// </summary>
    public class UploadImageHandler : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
    //不知道为什么获取不到
    //HttpPostedFile file = context.Request.Files["userFile"];
    string filePath = context.Request["imgPath"];
    string path = "UploadImgs\";
    Bitmap map = new Bitmap(filePath);
    string fileName = Path.GetFileName(filePath);
    string mapPath = context.Server.MapPath("~");
    string savePath = mapPath + "\" + path + fileName;
    map.Save(savePath);
    //上传成功后显示IMG文件
    StringBuilder sb = new StringBuilder();
    sb.Append("<img id="imgUpload" src="" + path.Replace("\", "/") + fileName + "" />");
    context.Response.Write(sb.ToString());
    context.Response.End();
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

  • 相关阅读:
    深入JAVA注解之属性注解
    深入JAVA注解之方法注解
    C# 启动外部程序的几种方法
    在.NET中实现彩色光标/动画光标和自定义光标[转]
    C#实现汉诺塔问题
    ExecuteNonQuery()返回值注意点
    在VS2012下不安装VS2010编译VS2010的工程
    Windows 窗体的.Net 框架绘图技术
    使用DataSet Datatable 更新数据库的三种方式
    C#.net 之货币转换
  • 原文地址:https://www.cnblogs.com/haofaner/p/5855425.html
Copyright © 2011-2022 走看看