ashx上传图片
㈠ ajax+jquery+ashx如何实现上传文件
第一:建立Default.aspx页面
<html>  
<head runat="server">  
    <title>ajax图片上传</title>  
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>  
    <script src="js/jquery.form.js" type="text/javascript"></script>  
      
    <script type="text/javascript">        
      function upload(){  
        var path = document.getElementById("File1").value;  
        var img = document.getElementById("img1");  
        if($.trim(path)==""){  
            alert("请选择要上传的文件");  
            return;  
            }  
              
        $("#form1").ajaxSubmit({  
            success: function (str) {   
                if(str!=null && str!="undefined"){  
                    if (str == "1") {alert("上传成功");document.getElementById("img1").src="images/logo.jpg?"+new Date();/*上传后刷新图片*/}  
                    else if(str=="2"){alert("只能上传jpg格式的图片");}  
                    else if(str=="3"){alert("图片不能大于1M");}  
                    else if(str=="4"){alert("请选择要上传的文件");}  
                    else {alert('操作失败!');}  
                }  
                else alert('操作失败!');  
            },  
            error: function (error) {alert(error);},  
            url:'Handler.ashx', /*设置post提交到的页面*/  
            type: "post", /*设置表单以post方法提交*/  
            dataType: "text" /*设置返回值类型为文本*/  
        });  
    }        
    </script>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <input id="File1" name="File1" type="file" />  
        <input id="iptUp" type="button" value="上传Logo"  onclick="upload()"/>  
        <img id="img1" alt="网站Logo" src="images/weblogo.jpg" />  
    </form>  
</body>  
</html> 
 
二、新建一个一般处理文件Handler.ashx
<%@ WebHandler Language="C#" Class="Handler" %>  
  
using System;  
using System.Web;  
  
public class Handler : IHttpHandler {  
      
    public void ProcessRequest (HttpContext context) {  
        HttpPostedFile _upfile = context.Request.Files["File1"];  
        if (_upfile == null)  
        {  
            ResponseWriteEnd(context, "4");//请选择要上传的文件   
        }  
        else  
        {  
            string fileName = _upfile.FileName;/*获取文件名: C:\Documents and Settings\Administrator\桌面\123.jpg*/  
            string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写: jpg*/  
            int bytes = _upfile.ContentLength;//获取文件的字节大小   
  
            if (suffix != "jpg")  
                ResponseWriteEnd(context, "2"); //只能上传JPG格式图片   
            if (bytes > 1024 * 1024)  
                ResponseWriteEnd(context, "3"); //图片不能大于1M   
  
            _upfile.SaveAs(HttpContext.Current.Server.MapPath("~/images/logo.jpg"));//保存图片   
            ResponseWriteEnd(context, "1"); //上传成功   
        }  
    }  
  
    private void ResponseWriteEnd(HttpContext context, string msg)  
    {  
        context.Response.Write(msg);  
        context.Response.End();  
    }  
      
    public bool IsReusable {  
        get {  
            return false;  
        }  
    }  
}  
 
您可以按照自己的需求来修改,希望对您有帮助!
㈡ 当点击带+的图片时,弹出手机相册,可以选择手机相册内的图片上传,代码如何写怎么实现
首先需要引用 js 和css
<!--上传控件-->
2<scriptsrc="query.form.min.js"></script>
3<linkhref="Img_List.css"rel="stylesheet"/>
4<scriptsrc="Img_List.js"></script>
Img_List.js 如下:
///<referencepath="../../script/jquery-1.8.0.min.js"/>
//显示
functionimgshow(obj){
//$(obj).find("a").show();
}
//隐藏
functionimghide(obj){
//$(obj).find("a").hide();
}
//上传
functionupload(){
$("#FileLoad").click();
}
//删除
functionimgdel(listId,FileId,hfId){
$.post("/CommonMole/ashx/public.ashx?action=DelMessageImg&Files="+$("#"+hfId).val(),function(result){
if(result!="ok")
$.messager.alert("消息提示","删除失败!");
});
varhtml="<li><imgsrc="/Themes/Images/jia.jpg"style="height:80px;width:80px;"/><inputtype="file"id=""+FileId+""class="input"onchange="ImgUpload('"+FileId+"','"+hfId+"','"+listId+"');"name=""+FileId+""/></li>"
$("#"+listId).html(html);
}
//添加成功
functionimgaddhtml(data,code,listId,fileId,hfId){
varlist=data.split(',');
varhtml="<lionmouseover="imgshow(this)"onmouseout="imghide(this);">";
html+="<astyle="height:80px;width:80px;"href=""+list[0]+""target="_blank"><imgsrc=""+list[0]+""imgs=""+list[0]+""code=""+code+""/></a><spanonclick="imgdel('"+listId+"','"+fileId+"','"+hfId+"')"></span></li>";
$("#"+listId).html(html);
}
//图片文件上传
//uppath上传空间id
//上传成功存放的图片路径的隐藏域id
//listId显示图片的区域id
functionImgUpload(uppath,hndimg,listId){
varsendUrl="/CommonMole/ashx/Upload_Ajax.ashx?action=SingleFile&IsThumbnail=1&UpFilePath="+uppath;
//开始提交
$("#form1").ajaxSubmit({
beforeSubmit:function(formData,jqForm,options){
//alert(1);
},
success:function(data,textStatus){
varlist=$("#"+hndimg).val();
$("#"+hndimg).val(data.msgbox);
imgaddhtml(data.msgbox,0,listId,uppath,hndimg);
},
error:function(data,status,e){
alert("上传失败!");
},
url:sendUrl,
type:"post",
dataType:"json",
timeout:600000
});
};
Img_List.css如下:
.img_list{margin:0px;padding:0px;overflow:hidden;}
.img_listul,.img_listulli{margin:0px;padding:0px;}
.img_listulli{float:left;list-style:none;position:relative;margin:5px0px0px5px;}
.img_listullispan
{position:absolute;top:3px;right:3px;width:16px;height:16px;opacity:0.6;filter:alpha(opacity=60);margin:0002px;
vertical-align:top;background:url('/Themes/Images/panel_tools.png')no-repeat-16px0px;}
.img_listulliimg{width:80px;height:80px;cursor:pointer;position:relative;z-index:0;}
.img_listulli.input{width:80px;height:80px;cursor:pointer;position:relative;left:-100px;vertical-align:top;margin:0px;padding:0px;opacity:0;filter:alpha(opacity=0);}
panel_tools.png 如下:
 #region上传单文件处理===================================
#region上传单文件处理===================================
46privatevoidSingleFile(HttpContextcontext)
47{
48
49string_refilepath=context.Request.QueryString["ReFilePath"];//取得返回的对象名称
50string_upfilepath=context.Request.QueryString["UpFilePath"];//取得上传的对象名称
51string_delfile=context.Request.QueryString[_refilepath];
52HttpPostedFile_upfile=null;
53try
54{
55_upfile=context.Request.Files[_upfilepath];
56}
57catch(Exceptione)
58{
59context.Response.Write("{"msg":"0","msgbox":"上传文件过大!"}");
60context.Response.End();
61}
62bool_iswater=false;//默认不打水印
63bool_isthumbnail=false;//默认不生成缩略图
64bool_isimage=false;
65
66if(context.Request.QueryString["IsWater"]=="1")
67_iswater=true;
68if(context.Request.QueryString["IsThumbnail"]=="1")
69_isthumbnail=true;
70if(context.Request.QueryString["IsImage"]=="1")
71_isimage=true;
72
73if(_upfile==null)
74{
75context.Response.Write("{"msg":"0","msgbox":"请选择要上传文件!"}");
76return;
77}
78UpLoapFiles=newUpLoad();
79stringmsg=upFiles.fileSaveAs(_upfile,_isthumbnail,_iswater,_isimage);
80//删除已存在的旧文件
81Utils.DeleteUpFile(_delfile);
82//返回成功信息
83context.Response.Write(msg);
84
85context.Response.End();
86}
87#endregion
101publicstringfileSaveAs(HttpPostedFilepostedFile,boolisThumbnail,boolisWater,bool_isImage,bool_isReOriginal)
102{
103try
104{
105stringfileExt=Utils.GetFileExt(postedFile.FileName);//文件扩展名,不含“.”
106stringoriginalFileName=postedFile.FileName.Substring(postedFile.FileName.LastIndexOf(@"")+1);//取得文件原名
107stringfileName=Utils.GetRamCode()+"."+fileExt;//随机文件名
108stringdirPath=GetUpLoadPath();//上传目录相对路径
109
110//检查文件扩展名是否合法
111if(!CheckFileExt(fileExt))
112{
113return"{"msg":"0","msgbox":"不允许上传"+fileExt+"类型的文件!"}";
114}
115//检查是否必须上传图片
116if(_isImage&&!IsImage(fileExt))
117{
118return"{"msg":"0","msgbox":"对不起,仅允许上传图片文件!"}";
119}
120//检查文件大小是否合法
121if(!CheckFileSize(fileExt,postedFile.ContentLength))
122{
123return"{"msg":"0","msgbox":"文件超过限制的大小啦!"}";
124}
125//获得要保存的文件路径
126stringserverFileName=dirPath+fileName;
=dirPath+"small_"+fileName;
128stringreturnFileName=serverFileName;
129//物理完整路径
130stringtoFileFullPath=Utils.GetMapPath(dirPath);
131//检查有该路径是否就创建
132if(!Directory.Exists(toFileFullPath))
133{
134Directory.CreateDirectory(toFileFullPath);
135}
136//保存文件
137postedFile.SaveAs(toFileFullPath+fileName);
138//如果是图片,检查图片尺寸是否超出限制
139if(IsImage(fileExt))
140{
141Thumbnail.MakeThumbnailImage(toFileFullPath+fileName,toFileFullPath+fileName,3000,3000);
142}
143//是否生成缩略图
144if(IsImage(fileExt)&&isThumbnail)
145{
146Thumbnail.MakeThumbnailImage(toFileFullPath+fileName,toFileFullPath+"small_"+fileName,150,150,"R");
147//returnFileName+=","+serverThumbnailFileName;//返回缩略图,以逗号分隔开
148}
149
166/
171return"{"msg":"1","msgbox":""+returnFileName+""}";
172}
173catch
174{
175return"{"msg":"0","msgbox":"上传过程中发生意外错误!"}";
176}
177}
㈢ extjs怎样做图片上传
1.代码如下:
复制代码
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4     <title></title>
5     <!--ExtJs框架开始-->
6     <script type="text/javascript" src="/Ext/adapter/ext/ext-base.js"></script>
7     <script type="text/javascript" src="/Ext/ext-all.js"></script>
8     <script src="/Ext/src/locale/ext-lang-zh_CN.js" type="text/javascript"></script>
9     <link rel="stylesheet" type="text/css" href="/Ext/resources/css/ext-all.css" />
10     <!--ExtJs框架结束-->
11     <script type="text/javascript">
12         Ext.onReady(function () {
13             //初始化标签中的Ext:Qtip属性。
14             Ext.QuickTips.init();
15             Ext.form.Field.prototype.msgTarget = 'side';
16             //创建div组件
17             var imagebox = new Ext.BoxComponent({
18                 autoEl: {
19                     style: 'width:150px;height:150px;margin:0px auto;border:1px solid #ccc; text-align:center;padding-top:20px;margin-bottom:10px',
20                     tag: 'div',
21                     id: 'imageshow',
22                     html: '暂无图片'
23                 }
24             });
25             //创建文本上传域
26             var file = new Ext.form.TextField({
27                 name: 'imgFile',
28                 fieldLabel: '文件上传',
29                 inputType: 'file',
30                 allowBlank: false,
31                 blankText: '请浏览图片'
32             });
33             //提交按钮处理方法
34             var btnsubmitclick = function () {
35                 if (form.getForm().isValid()) {
36                     form.getForm().submit({
37                         waitTitle: "请稍候",
38                         waitMsg: '正在上传...',
39                         success: function (form, action) {
40                             Ext.MessageBox.alert("提示", "上传成功!");
41                             document.getElementById('imageshow').innerHTML = '<img style="width:150px;height:150px" src="' + action.result.path + '"/>';
42                         },
43                         failure: function () {
44                             Ext.MessageBox.alert("提示", "上传失败!");
45                         }
46                     });
47                 }
48             }
49             //重置按钮"点击时"处理方法
50             var btnresetclick = function () {
51                 form.getForm().reset();
52             }
53             //表单
54             var form = new Ext.form.FormPanel({
55                 frame: true,
56                 fileUpload: true,
57                 url: '/App_Ashx/Demo/Upload.ashx',
58                 title: '表单标题',
59                 style: 'margin:10px',
60                 items: [imagebox, file],
61                 buttons: [{
62                     text: '保存',
63                     handler: btnsubmitclick
64                 }, {
65                     text: '重置',
66                     handler: btnresetclick
67                 }]
68             });
69             //窗体
70             var win = new Ext.Window({
71                 title: '窗口',
72                 width: 476,
73                 height: 374,
74                 resizable: true,
75                 modal: true,
76                 closable: true,
77                 maximizable: true,
78                 minimizable: true,
79                 buttonAlign: 'center',
80                 items: form
81             });
82             win.show();
83         });
84     </script>
85 </head>
86 <body>
87 <!--
88 说明:
89 (1)var imagebox = new Ext.BoxComponent():创建一个新的html标记。
90     官方解释如下:
91     This may then be added to a Container as a child item.
92     To create a BoxComponent based around a HTML element to be created at render time, use the autoEl config option which takes the form of a DomHelper specification:
93 (2) autoEl: {style: '',tag: 'div',id: 'imageshow', html: '暂无图片'}定义这个html标记的属性,如 标记为:div,id是多少等。
94     官方实例为:
95     var myImage = new Ext.BoxComponent({
96     autoEl: {
97         tag: 'img',
98         src: '/images/my-image.jpg'
99         }
100     });
101 (3)var file = new Ext.form.TextField():创建一个新的文件上传域。
102 (4)name: 'imgFile':名称,重要,因为service端要根据这个名称接收图片。
103 (5)inputType: 'file':表单类型为文件类型。
104 (6)waitTitle: "请稍候",waitMsg: '正在上传...',:上传等待过程中的提示信息。
105 (7)document.getElementById('imageshow').innerHTML = '<img style="width:150px;height:150px" src="' + action.result.path + '"/>';这个是原生态的js,把imageshow的值换成图片。
106 -->
107 </body>
108 </html>
复制代码
其中与service交互用上传图片的 一般处理程序文件,源码如下:
/App_Ashx/Demo/Upload.ashx
复制代码
1 using System;
2 using System.Web;
3 using System.IO;
4 using System.Globalization;
5
6 namespace HZYT.ExtJs.WebSite.App_Ashx.Demo
7 {
8     public class Upload : IHttpHandler
9     {
10         public void ProcessRequest(HttpContext context)
11         {
12             //虚拟目录,建议写在配置文件中
13             String strPath = "/Upload/Image/";
14             //文件本地目录
15             String dirPath = context.Server.MapPath(strPath);
16             //接收文件
17             HttpPostedFile imgFile = context.Request.Files["imgFile"];
18             //取出文件扩展名
19             String fileExt = Path.GetExtension(imgFile.FileName).ToLower();
20             //重新命名文件
21             String newFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;
22             //文件上传路径
23             String filePath = dirPath + newFileName;
24             //保存文件
25             imgFile.SaveAs(filePath);
26             //客户端输出
27             context.Response.Write("{success:true,path:'" + strPath + newFileName + "'}");
28         }
29
30         public bool IsReusable
31         {
32             get
33             {
34                 return false;
35             }
36         }
37     }
38 }
复制代码
2.效果如下:
无废话extjs
3.说明:
(1)上传域不光可以上传图片,还要以上传其他文件。这里我们以图片为例。
(2)在实际开发中,我们还要对图片格式,大小等进行校验,这个示例测重于上传,没有加入任何校验。
㈣ asp.net 一般处理程序(ashx)如何多次接收上传文件(多文件批量上传)
Like this :
比如前台有3个INPUT: 
<form enctype= "multipart/form-data">
    <INPUT   style= "WIDTH:   480px;   HEIGHT:   22px "   type= "file "   name= "File "   size= "60 "> 
    <INPUT   style= "WIDTH:   480px;   HEIGHT:   22px "   type= "file "   name= "File "   size= "60 "> 
    <INPUT   style= "WIDTH:   480px;   HEIGHT:   22px "   type= "file "   name= "File "   size= "60 "> 
</form>
然后后台:
HttpFileCollection   files     =   HttpContext.Current.Request.Files; 
//这个files里面就是你上传文件的集合。遍历即可。
㈤ jquery+ajax+ashx 图片上传问题,各位大神帮忙看看这样写对吗
图片上传需要使用表单提交,ajax 提交是无法提交file的。建议使用Form,然后ajaxsubmit
㈥ ueditor .net图片上传不成功
这个问题很好解决。
只要删除ashx文件里第一行代码就行了。具体删除哪一行,我现在也不知道了,因为已经删除了。
不过我可以给你看看删除后的代码,你对比一下,多了哪一行就把哪一行删除就行。
ueditor下面的net下面所有ashx文件都要删除代码。
以imageUp.ashx为例子。
<%@WebHandlerLanguage="C#"Class="imageUp"%>
usingSystem;
usingSystem.Web;
usingSystem.IO;
usingSystem.Collections;
㈦ aspx页面中有一些数值,把它传个ashx页面,ashx页面动态生成一张图片
这个简单多了,只要你能够传一个获取byte数组的参数到ashx页面,然后在里面Response.AddHeader("Content-type", "image/jpeg");
Response.BinaryWrite(buffer);
就可以生成一张图片了,如果想调用这张图片就把image控件的src设置为ashx的路径即可。
㈧ 用jquery实现ajax 上传图片提交到PHP
好像不可以吧,浏览器为了安全不能用javascript读取本地文件的
㈨ kindeditor-4.1.3中ASP.NET上传图片配置
我也遇到过,如果在引用里添加了LitJSON.dll的话,仔细检查下面的路径
<head runat="server">
    <title>add news</title>
    <link href="../kindeditor-4.1.7/themes/default/default.css" rel="stylesheet" type="text/css" />
    <link href="../kindeditor-4.1.7/plugins/code/prettify.css" rel="stylesheet" type="text/css" />
    <script src="../kindeditor-4.1.7/kindeditor.js" type="text/javascript"></script>
    <script src="../kindeditor-4.1.7/lang/zh_CN.js" type="text/javascript"></script>
    <script src="../kindeditor-4.1.7/plugins/code/prettify.js" type="text/javascript"></script>
    <script>
        KindEditor.ready(function (K) {
            var editor1 = K.create('#content1', {
                cssPath: '../kindeditor-4.1.7/plugins/code/prettify.css',
                uploadJson: '../kindeditor-4.1.7/asp.net/upload_json.ashx',
                fileManagerJson: '../kindeditor-4.1.7/asp.net/file_manager_json.ashx',
                allowFileManager: true,
                afterCreate: function () {
                    var self = this;
                    K.ctrl(document, 13, function () {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                    K.ctrl(self.edit.doc, 13, function () {
                        self.sync();
                        K('form[name=example]')[0].submit();
                    });
                }
            });
            prettyPrint();
        });
    </script>
</head>
尤其是这两句
 uploadJson: '../kindeditor-4.1.7/asp.net/upload_json.ashx',
  fileManagerJson: '../kindeditor-4.1.7/asp.net/file_manager_json.ashx',
㈩ ueditor .net 1.4.3 怎么把图片上传路径传给controller.ashx
在ueditor.config.js中修改服务器统一请求接口路径
         serverUrl: URL + "net/controller.ashx",链接到你controller所在的位置
