为了方便多出调用图片上传方法       首先我们将图片上传方法抽离出来

创建ashx 一个新的方法

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web; namespace FileImg
{
/// <summary>
/// ImgUpLoad 的摘要说明
/// </summary>
public class ImgUpLoad : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
} public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 图片上传 并保存至服务器 返回 保存图片的相对路径URL
/// </summary>
/// <param name="context"></param>
/// <param name="file"></param>
/// <returns></returns>
public string ImgUp(HttpContext context)
{
//上传的第一个文件
HttpPostedFile file = context.Request.Files[];
//获取图片的名称
string ImgName = file.FileName;
//获取图片的扩展名
string ImgExtention = System.IO.Path.GetExtension(ImgName);
//将图片流文件保存在 字节序列容器 中
Stream stream = file.InputStream;
//将图片流文件转换为Image图片对象
Image img = Image.FromStream(stream); //将图片保存在服务器上 //为了防止图片名称重复 我们使用随机数命名
Random ran = new Random((int)DateTime.Now.Ticks);
//图片保存的目录 按照日期进行保存
string subPath = "/imgUploads/" + DateTime.Now.ToString("yyyyMMdd") + "/"; // 20190928
//图片保存的目录的绝对路径
string path = context.Server.MapPath(subPath);
//当文件夹路径不存在时 创建文件夹
if (false == System.IO.Directory.Exists(path))
{
//创建pic文件夹
System.IO.Directory.CreateDirectory(path);
}
string imgName = ran.Next() + ImgExtention;
string serverPath = path + imgName;//文件保存位置及命名
string imgPath = subPath + imgName;
try
{
img.Save(serverPath);
return imgPath;
}
catch
{
return "";
}
} }
}

在你所要用到的方法里面进行调用上面的图片上传方法   这里可以接收到图片上传方法返回的  已经上传的图片的相对路径

   
ImgUpLoad load = new ImgUpLoad();
string imgUrl = load.ImgUp(context);

ashx 图片上传的更多相关文章

  1. ashx 图片上传路径URL

    ashx 图片上传   为了方便多出调用图片上传方法       首先我们将图片上传方法抽离出来 创建ashx 一个新的方法 using System; using System.Collection ...

  2. ashx图片上传接收

    发送数据流方法 /// <summary> /// PostBinaryData /// </summary> /// <param name="url&quo ...

  3. bootstrap-fileinput 图片上传

    bootstrap-fileinput 源文件 在网上下载 CSS: <link href="../../static/Bootstrap/css/plugins/bootstrap- ...

  4. asp.net 百度编辑器 UEditor 上传图片 图片上传配置 编辑器配置 网络连接错误,请检查配置后重试

    1.配置ueditor/editor_config.js文件,将 //图片上传配置区 ,imageUrl:URL+"net/imageUp.ashx" //图片上传提交地址 ,im ...

  5. CKEditor不借助CKFinder实现图片上传(.net版 ashx实现)

    参考博客:http://blog.csdn.net/mydwr/article/details/8669594 本人版本:4.4.6 打开文件:ckeditor/plugins/image/dialo ...

  6. ajax图片上传(asp.net +jquery+ashx)

    一.建立Default.aspx页面 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile=&q ...

  7. 对百度的UEditor多图片上传的一些补充

    我已经写了一篇文章关于百度的UEditor提取多图片上传模块.如果还没有看过,请点击以下链接查看 http://www.cnblogs.com/luke1006/p/3719029.html 出差了两 ...

  8. 百度UEditor在线编辑器的配置和图片上传

    前言 最近在项目中使用了百度UEditor富文本编辑器,配置UEditor过程中遇到了几个问题,在此记录一下解决方案和使用方法,避免以后使用UEditor出现类似的错误. 基本配置 一.下载UEdit ...

  9. 关于editor网页编辑器ueditor.config.js 配置图片上传

    最近公司项目在做一个门户网站,其中新闻和简介等部分使用到了ueditor编辑器,但是上级明确指示需要图片上传这个功能,这时却发现图片上传功能不能正常使用,上传时一直报错,网上收了好几个处理办法,都说的 ...

随机推荐

  1. STM32之复用功能

    复用功能分复用输入,复用输出,STM32芯片内部集成多种模块,如GPIO.串口.i2c等,为使IO端口支持这些模块,厂家对IO端口进行扩展,同一个端口通过设置寄存器会有不同的功能.如下图IO结构图: ...

  2. Linux基础-15-samba服务

    1. samba的功能: samba是一个网络服务器,用于Linux和Windows之间共享文件. 2. samba服务的启动.停止.重启 service smb start|stop|restart ...

  3. Django基础之django分页

    一.Django的内置分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views ...

  4. ps 指令

    ps显示系统当前进程信息, ps 存在多个版本,因此 ps options 的种类繁多.这里只列举平时开发过程中常用的命令,如果有错误或者更好的例子.烦请在评论区指出 语法 ps [options] ...

  5. CentOS7 安装 Git

    环境: 系统版本:CentOS 7.5 Git 版本:2.20.1 一.安装 Git 1.下载编译工具 $ yum -y groupinstall "Development Tools&qu ...

  6. CentOS && Ubuntu 环境下 Docker 的安装配置

    CentOS 7 install Docker Docker 支持的 centos 版本:CentOS 6.5(64-bit)或更高的版本 使用 yum 安装 1)确保 yum 包更新到最新 [roo ...

  7. 一、hystrix如何集成在openfeign中使用

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 HystrixInvocationHandler hystrix是开源的一个熔断组件,s ...

  8. Eclipse-错误集

    1.The origin server did not find a current representation for the target resource or is not willing ...

  9. selenium异常

    记一下让我花时间去找解决办法的异常 org.openqa.selenium.ElementNotInteractableException: element not interactable 第一次出 ...

  10. linux防火墙扩展模块实战(二)

    iptables扩展模块    扩展匹配条件:需要加载扩展模块(/usr/lib64/xtables/*.so),方可生效 查看帮助 man iptables-extensions (1)隐式扩展 ...