asp.net上传图片

有时候在网站中需要引入文本编辑器的样式,但是3.0去除样式,导致前台文本一大堆,这是效果真是太糟了。

效果图

<div style="width: 100%">

<div id="div1" style="height: 800px;">
<%--<p>请输入内容...</p>--%>
<%=information_Content%>
</div>
</div>

<script type="text/javascript">
var editor = new wangEditor('div1');
editor.config.uploadImgUrl = 'upload.ashx';
editor.config.hideLinkImg = true;
editor.config.uploadHeaders = {
'Accept': 'text/x-json'
};
editor.create();
</script>

upload.ashx代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace nsd.admin.ashx
{
/// <summary>
/// upload 的摘要说明
/// </summary>
public class upload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        var files = context.Request.Files;
        if (files.Count <= 0)
        {
            return;
        }

        HttpPostedFile file = files[0];

        if (file == null)
        {
            context.Response.Write("error|file is null");
            return;
        }
        else
        {
            string path = context.Server.MapPath("~/admin/upload/");  //存储图片的文件夹  
            string originalFileName = file.FileName;
            string fileExtension = originalFileName.Substring(originalFileName.LastIndexOf('.'), originalFileName.Length - originalFileName.LastIndexOf('.'));
            string currentFileName = (new Random()).Next() + fileExtension;  //文件名中不要带中文,否则会出错  
            //生成文件路径  
            string imagePath = path + currentFileName;
            //保存文件  
            file.SaveAs(imagePath);

            //获取图片url地址  
            string imgUrl = "upload/" + currentFileName;

            //返回图片url地址  
            context.Response.Write(imgUrl);
            return;
        }  
    }

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

}
唯一难点就是路径问题会导致错误:

找不到一般处理程序

这是我的路径,和页面在一个路径

image.png

修改后,效果:

效果图

补充:
路径的设置:
//获取图片url地址
//string imgUrl = "upload/" + currentFileName;
string imgUrl = "http://localhost:3697/admin/upload/" + currentFileName;

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容