博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uploadfy 图片/视频上传
阅读量:5268 次
发布时间:2019-06-14

本文共 8422 字,大约阅读时间需要 28 分钟。

  JS引入

<link href="../../Scripts/uploadify/uploadify.css" rel="stylesheet" />

<script src="../../Scripts/uploadify/jquery.uploadify.js" type="text/javascript"></script>

html

<tr>

<td align="left" class="tdTextValue">图片:</td>
<td align="left" class="tdDataValue">
<input name="file_upload" type="file" id="file_upload" value="" />
<input id="PicList" name="PicList" type="hidden" />
<ul class="picUpList2" id="preview">
</ul>
</td>
</tr>
<tr>
<td align="left" class="tdTextValue">视频:</td>
<td align="left" class="tdDataValue">
<input name="file_upload1" type="file" id="file_upload1" value="" />
<input id="PicList1" name="PicList1" type="hidden" />
<ul class="picUpList2" id="preview1">
</ul>
</td>
</tr>

  JS代码

<script language="javascript" type="text/javascript">

 

$(function () {

uploadify();
uploadify1();
});

 

//上传图片

function uploadify() {
$("#preview").html("");
$("#file_upload").uploadify({
uploader: '/Json/UploadHandler.ashx',
swf: '/Scripts/uploadify/uploadify.swf',
folder: '/UploadFiles/Images',
buttonText: "添加文件",
height: 24,
width: 70,
multi: false,
//uploadLimit: 1,
fileTypeExts: "*.jpg;*.png;*.gif;*.bmp",
fileTypeDesc: "选择图片",
onFallback: function () {
alert("您未安装FLASH控件,无法上传!请安装FLASH控件后再试。");
},
onUploadSuccess: function (file, data, response) {

$("#preview").html("");

var DataJson = JSON.parse(data);

$("<li><img width=\"80\" height=\"80\" src=\"" + DataJson.FilePath.replace("~", "") + "\"><a class=\"delIcon1\" οnclick=\"RemoveImg(this)\" href=\"javascript:void(0);\"></a></li>").appendTo($("#preview"));

if ($("#PicList").val() == "" || $("#PicList").val() == null) {
$("#PicList").val(DataJson.FilePath.replace("~", ""));
} else {
$("#PicList").val(DataJson.FilePath.replace("~", ""));
}
}
});
}

 

function RemoveImg(obj) {

if (confirm("确认删除此图片吗?")) {

getAjax("/Json/UploadHandler.ashx", { DelfileName: $(obj).prev().attr("src") }, function (data) {

var JsonData = eval("(" + data + ")");

if (JsonData.Code == "1") {

$("#PicList").val($("#PicList").val().replace($(obj).prev().attr("src"), ""));

var _preview = "<li><img width=\"80\" height=\"80\" src=\"" + $(obj).prev().attr("src") + "\"><a class=\"delIcon1\" οnclick=\"RemoveImg(this)\" href=\"javascript:void(0);\"></a></li>";
$("#preview").html($("#preview").html().replace(_preview, ""));

alert("图片删除成功!");

}
else if (JsonData.Code == "-1") {
alert(JsonData.Message);
return;
}
else {
$("#PicList").val($("#PicList").val().replace($(obj).prev().attr("src"), ""));
var _preview = "<li><img width=\"80\" height=\"80\" src=\"" + $(obj).prev().attr("src") + "\"><a class=\"delIcon1\" οnclick=\"RemoveImg(this)\" href=\"javascript:void(0);\"></a></li>";
$("#preview").html($("#preview").html().replace(_preview, ""));

alert("没找到相应文件,图片删除失败!");

return;
}
});
}

}

 

//上传视频

function uploadify1() {
$("#preview1").html("");
$("#file_upload1").uploadify({
uploader: '/Json/UploadHandler.ashx',
swf: '/Scripts/uploadify/uploadify.swf',
folder: '/UploadFiles/Images',
buttonText: "添加文件",
height: 24,
width: 70,
//multi: false,
//uploadLimit: 1,
fileSizeLimit: 0,
fileTypeExts: "*.flv;*.mp4",
fileTypeDesc: "选择视频",
onFallback: function () {
alert("您未安装FLASH控件,无法上传!请安装FLASH控件后再试。");
},
onUploadSuccess: function (file, data, response) {

$("#preview1").html("");

var DataJson = JSON.parse(data);
$("<li><span>" + DataJson.filename + "</span><a class=\"delIcon2\" οnclick=\"RemoveImg1('" + DataJson.filename + "')\" href=\"javascript:void(0);\"></a></li>").appendTo($("#preview1"));
;
if ($("#PicList1").val() == "" || $("#PicList1").val() == null) {
$("#PicList1").val(DataJson.filename + ",");
} else {
$("#PicList1").val($("#PicList1").val() + DataJson.filename + ",");
}
}
});
}

 

function RemoveImg1(name) {

if (confirm("确认删除此视频文件吗?")) {
getAjax("/Json/UploadHandler.ashx", { DelfileName: "/UploadFiles/Images/" + name }, function (data) {
var JsonData = eval("(" + data + ")");
if (JsonData.Code == "1") {

$("#PicList1").val($("#PicList1").val().replace(name + ",", ""));

var _preview1 = "<li><span>" + name + "</span><a class=\"delIcon2\" οnclick=\"RemoveImg1('" + name + "')\" href=\"javascript:void(0);\"></a></li>";
$("#preview1").html($("#preview1").html().replace(_preview1,""));

alert("视频删除成功!");

}
else if (JsonData.Code == "-1") {
alert(JsonData.Message);
return;
}
else {
$("#PicList1").val($("#PicList1").val().replace(name + ",", ""));
var _preview1 = "<li><span>" + name + "</span><a class=\"delIcon2\" οnclick=\"RemoveImg1('" + name + "')\" href=\"javascript:void(0);\"></a></li>";
$("#preview1").html($("#preview1").html().replace(_preview1, ""));

alert("没找到相应文件,视频删除失败!");

return;
}
});
}

}

 

</script>

后台代码(我用的是一般处理程序文件)

public class UploadHandler : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{
context.Response.ContentType = "text/plain";
context.Response.Charset = "utf-8";

string fileName = context.Request.Form["DelfileName"] == null ? "" : context.Request.Form["DelfileName"].ToString();

if (fileName != "") //删除图片
{
string virtualPath = string.Format("~{0}", fileName);
string strPath = HttpContext.Current.Server.MapPath(virtualPath);
string strJson = "";
try
{
if (!System.IO.File.Exists(strPath))
{
strJson = "{ \"Success\":false, \"Code\":\"-2\", \"Message\":\"没找到相应文件,图片删除失败!\" }";
}
else
{
System.IO.File.Delete(strPath);
strJson = " { \"Success\":true, \"Code\":\"1\", \"Message\":\"图片删除成功!\" }";
}

}

catch (Exception ex)
{
strJson = "{ \"Success\":false, \"Code\":\"-1\", \"Message\":\"操作失败:\" " + ex.Message + " }";
}
context.Response.Write(strJson);
}
else
{
HttpPostedFile file = context.Request.Files["Filedata"];
string uploadPath = HttpContext.Current.Server.MapPath("/UploadFiles/Images/") + "\\";
if (file != null)
{
string fileNameExit = file.FileName.Substring(file.FileName.LastIndexOf(".")).ToLower();
string strFileName = getFileName() + fileNameExit;
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}

file.SaveAs(uploadPath + strFileName);

string saveFile = uploadPath + strFileName;
string virtualPath = string.Format("~/UploadFiles/Images/{0}", strFileName);
string strJson = "{\"filename\":\"" + strFileName + "\",\"FilePath\":\"" + virtualPath + "\"}";

if (!(CheckTextFile(saveFile) == FileExtension.PNG || CheckTextFile(saveFile) == FileExtension.BMP || CheckTextFile(saveFile) == FileExtension.GIF || CheckTextFile(saveFile) == FileExtension.JPG || CheckTextFile(saveFile) == FileExtension.filelist || CheckTextFile(saveFile) == FileExtension.filexlist || CheckTextFile(saveFile) == FileExtension.rar || CheckTextFile(saveFile) == FileExtension.pdf || CheckTextFile(saveFile) == FileExtension.mp4 || CheckTextFile(saveFile) == FileExtension.flv))

{
File.Delete(saveFile);
strJson = "{\"filename\":\"\",\"FilePath\":\"\"}"; //扩展名不对
}

context.Response.Write(strJson);

}
else
{
//context.Response.Write("0");
context.Response.Write("{\"filename\":\"\",\"FilePath\":\"\"}");
}
}
}

//删除图片

public void DeleteUploadFile(HttpContext context,string fileName)
{
}

/// <summary>

/// 得到随机的文件名
/// </summary>
/// <returns></returns>
public static string getFileName()
{
Random rd = new Random();
StringBuilder serial = new StringBuilder();
serial.Append(DateTime.Now.ToString("yyyyMMddHHmmssff"));
serial.Append(rd.Next(0, 9999).ToString());
return serial.ToString();
}
public static FileExtension CheckTextFile(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
string fileType = string.Empty; ;
try
{
byte data = br.ReadByte();
fileType += data.ToString();
data = br.ReadByte();
fileType += data.ToString();
FileExtension extension;
try
{
extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
}
catch
{

extension = FileExtension.VALIDFILE;

}
return extension;
}
catch (Exception ex)
{
return FileExtension.VALIDFILE;
}
finally
{
if (fs != null)
{
fs.Close();
br.Close();
}
}
}

public enum FileExtension

{
JPG = 255216,
GIF = 7173,
PNG = 13780,
BMP = 6677,
rar = 8297,
pdf = 3780,
filelist = 208207,//xls.doc.ppt
filexlist = 8075, //xlsx,zip,pptx,docx,mmap,zip
flv = 7076,
mp4 = 00,
//apk = 8075,
//SWF = 6787,
//ZIP = 8075,
//_7Z = 55122,
VALIDFILE = 9999999
// 255216 jpg;

// 7173 gif;

// 6677 bmp,

// 13780 png;

// 6787 swf

// 7790 exe dll,

 

// 55122 7z

// 6063 xml

// 6033 html

// 239187 aspx

// 117115 cs

// 119105 js

// 102100 txt

// 255254 sql

}

public bool IsReusable

{
get
{
return false;
}
}
}

 

转载于:https://www.cnblogs.com/zqn518/p/10471523.html

你可能感兴趣的文章
Most Distant Point from the Sea UVALive - 3890(半平面交)
查看>>
javascript正则
查看>>
从AIDL开始谈Android进程间Binder通信机制
查看>>
android开发常用地址
查看>>
SSH框架整合配置所需JAR包(SSH整合)
查看>>
PHP函数
查看>>
html5多媒体Video/Audio
查看>>
宽高自适应
查看>>
如何安装windows7
查看>>
[主席树]HDOJ4348 To the moon
查看>>
shell脚本统计文件中单词的个数
查看>>
SPCE061A学习笔记
查看>>
sql 函数
查看>>
hdu 2807 The Shortest Path 矩阵
查看>>
高斯模糊的简单算法
查看>>
熟悉项目需求,要知道产品增删修改了哪些内容,才会更快更准确的在该项目入手。...
查看>>
JavaScript 变量
查看>>
java实用类
查看>>
20170531
查看>>
图片预加载之比onload更快的获取图片尺寸
查看>>