c#初级程序员,之前在完成图片上传功能的时候,对于前台功能的上传回显有很多资源,但是结合c#将对象去传递给后台的资料缺特别的少,而且提供的方法都不是很完整,既要了解前台功能编写的原理,对于我这更开始使用c#语言的人那可是废了很大的功夫。好歹功夫不负有心人,东拼西凑总算是把功能实现了,这里把实现的方法分享一下,以便大家快速解决问题。
前台展示效果
前台界面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="FileSave.FileUpload" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery带删除、放大功能多图片上传预览插件</title>
<link href="css/common.css" type="text/css" rel="stylesheet" />
<link href="css/index.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="JS/jquery-1.8.3.min.js"></script>
<script>
function upload() {
var form = document.getElementById("form");
var formdata = new FormData(form);
formdata.append("fileArray", fileArray[0]);
$.ajax({
url: "FileUpload.ashx",
type: "POST",
data: formdata,
cache: false,
async: false,
processData: false,
contentType: false,
success: function () {
alert("上传成功")
},
error: function () {
}
})
}
</script>
</head>
<body>
<script src="/demos/googlegg.js"></script>
<form runat="server" id="form">
<div class="img-box full">
<section class=" img-section">
<p class="up-p">作品图片:<span class="up-span">最多可以上传5张图片,马上上传</span></p>
<div class="z_photo upimg-div clear">
<section class="z_file fl">
<img src="img/a11.png" class="add-img">
<input type="file" name="file" id="file" class="file" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" multiple />
</section>
</div>
</section>
</div>
<aside class="mask works-mask">
<div class="mask-content">
<p class="del-p ">您确定要删除作品图片吗?</p>
<p class="check-p"><span class="del-com wsdel-ok">确定</span><span class="wsdel-no">取消</span></p>
</div>
</aside>
<center> <input type="button" onclick="upload()" value="上传" /></center>
</form>
<div style="text-align: center; margin: 50px 0; font: normal 14px/24px 'MicroSoft YaHei';">
<p>适用浏览器:360、FireFox、Chrome、Safari、Opera、傲游、搜狗、世界之窗. 不支持IE8及以下浏览器。</p>
</div>
<script type="text/javascript" src="JS/imgUp.js"></script>
<script type="text/javascript" src="JS/jquery.form.js"></script>
</body>
</html>
后台
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace FileSave
{
/// <summary>
/// FileUpload1 的摘要说明
/// </summary>
public class FileUpload1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpFileCollection _file = HttpContext.Current.Request.Files;
//图片大小
long size = _file[0].ContentLength;
//图片类型
string type = _file[0].ContentType;
//图片名
string name = _file[0].FileName;
//图片格式
string _tp = System.IO.Path.GetExtension(name);
//创建新的图片名
string saveName = DateTime.Now.ToString("yyyyMMdd") + Guid.NewGuid().ToString() + _tp;
//寻找图片夹目录,没有则创建
string path = context.Server.MapPath("~/Uploadfileorimg/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
DirectoryInfo dirInfo = new DirectoryInfo(path);
dirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
}
//保存图片到指定路径
_file[0].SaveAs(path + saveName);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
所需要的辅助文件
http://39.104.206.57:8080/附件.rar