UploadHandler.ashx.cs
(开头部分) 4KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
namespace UploadEditPhoto
{
/// <summary>
/// 上传图片
/// create by kingtentx
/// 2014.11.14
/// </summary>
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.Files["photo"] == null)
{
//图片控件中没有选择图片
context.Response.Write("{state:'error',msg:'请选择图片'}");
}
else
{
HttpPostedFile imgFile= context.Request.Files["photo"];
if (imgFile.ContentLength > 1024 * 1000 * 2)//小于2M
{
context.Response.Write("{state:'error',msg:'上传图片不能超过2M'}");
return;
}
var filePath = "/Uploads/temp/"; //上传文件夹
string imageName = UpLoadImage(imgFile, filePath);
context.Response.Write("{state:'success',msg:'上传成功',path:'" + filePath + imageName + "'}");
}
}
catch (Exception ex)
{
context.Response.Write("{state:'error',msg:'" + ex.Message + "'}");
}
}
#region 上传图片
/// <summary>
/// 上传图片
/// </summary>
/// <param name="imgfile">文件http流</param>
/// <param name="nowpath">所需放置的文件路径</param>
/// <returns>上传成功,返回文件名,否则,抛出异常</returns>
public static string UpLoadImage(HttpPostedFile imageFile, string filePath)
{
try
{
//string serverPath = System.Web.HttpContext.Current.Server.MapPath("~");
后面还有 80 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
