ASP .NET MVC4 利用uploadify.js多文件上传

var fileName = $.text(),html xmlns=”,html xmlns=”,mail{

图片 1

  <link
href=”~/Scripts/uploadify/uploadify.css” rel=”stylesheet” />
<style type=”text/css”>
#upDiv {
width: 550px;
height: 400px;
border: 2px solid red;
margin-top: 30px;
margin-left: 50px;
float: left;
}

String fileUrl = saveUrl + newFileName;

String newFileName = DateTime.Now.ToString(“yyyyMMddHHmmss_ffff”,
DateTimeFormatInfo.InvariantInfo) + fileExt;
String filePath = dirPath + newFileName;

}
#photo img{
border:none;
height:380px;
width:630px;
}
#infomation{
padding:5px 0 0 0;
position:absolute;
top:400px;
left:25px;
height:105px;
width:629px;
}
#entry{
margin:10px 0 0 6px;
float:left;
width:108px;
height:90px;
}
#entry ul{
 margin: 0;
 padding: 0;
}
#entry li{
float:left;
list-style:none;
text-indent:10px;
}
#entry a{
display:block;
height:42px;
width:53px;
}
#cjhd{
backgroundnull:url() no-repeat top
left;
height:42px;
width:53px;
}
#aygw{
backgroundnull:url() no-repeat top
left;
height:42px;
width:53px;
}
#zdzx{
backgroundnull:url() no-repeat top
left;
height:42px;
width:53px;
}
#title{
height:21px;
margin:5px 0;
backgroundnull:url() no-repeat top
left;
}
#artical{
margin:0 0 0 5px;
float:left;
width:180px;
height:105px;
overflow:hidden;
}
#words{
font-size:14px;
height:70px;
line-height:18px;
margin:20px 0 0 0;
}
#words p{
margin:0;
padding:0;
}
#words a{
text-decoration:underline;
color:#be2f60;
}
#artical{
margin:5px 0 0 0px;
}
#artical ul{
padding:0;
margin:5px 0 0 5px;
}
#artical ul li{
list-style:none;
background:url(111cn.net/emailimages//emailimages/dot.gif) no-repeat 0px
7px;
text-indent:10px;
height:18px;
color:#505050;
float:left;
width:180px;

#filelist {
width: 45%;
height: 400px;
float: left;
}

 

String aspxUrl = context.Request.Path.Substring(0,
context.Request.Path.LastIndexOf;

我们来看看模板文件

<div >
<h3>图片列表</h3>
</div>
</div>
</body>

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
string operate = context.Request[“operate”];
if (operate == “deleteFile”)
{
#region 删除文件附件信息
//获取文件路径
string filePath = context.Server.MapPath(context.Request[“file”]);
//判断文件是否存在
if (File.Exists(filePath))
File.Delete(filePath);//删除文件
//获取附件cookie信息
HttpCookie fileCookie = context.Request.Cookies[“FileCookie”];
string[] fileArray = new string[1];
if (fileCookie != null)
{
filePath = filePath.Remove(0,
filePath.IndexOf(“upfiles”)).Replace(“\\”, “/”);
if (fileCookie.Value.Contains(“|”))
fileArray = fileCookie.Value.Split(‘|’);
else
fileArray[0] = fileCookie.Value;
string strFile = “”;
for (int i = 0; i < fileArray.Length; i++)
{
if (!fileArray[i].Contains(filePath))
strFile += fileArray[i] + “|”;
}
if (strFile.Contains(“|”))
strFile = strFile.Remove(strFile.Length – 1);
fileCookie.Value = strFile;
fileCookie.Expires = DateTime.Now.AddDays(1);
fileCookie.HttpOnly = true;
context.Response.AppendCookie(fileCookie);

HttpPostedFile imgFile = context.Request.Files[“imgFile”];
/*if (imgFile == null)
{
showError;
}*/

}
#artical ul li a{
text-decoration:none;
color:#5e5e5e;

 
 
 
</style>
</head>
<body>
<div id=”mail”>
 <div id=”photo”>   </div>
    <div id=”infomation”>
     <div id=”entry”>
         <ul>
             <li id=”cjhd”><a
href=”#”></a></li>
                <li id=”zdzx”><a
href=”#”></a></li>
                <li id=”aygw”><a
href=”#”></a></li>
            </ul>
        </div>
     <div id=”artical”>
         <div id=”title”>
            </div>
            <ul>
             <li><a href=”#”>新潮食物与儿童疾病的关系
</a></li>
                <li><a href=”#”>不宜喂养宝宝的24种食物 
</a></li>
                <li><a
href=”#”>育儿饮食错误观点大罗列</a></li>
            </ul>
        </div>
        <div id=”words”>
        <p>{username}说:</p>
        <p style=”text-indent:20px;”><a
href=”;
        </div>
     
    </div>
</div>

  博客地址:

String aspxUrl = context.Request.Path.Substring(0,
context.Request.Path.LastIndexOf(“/”) + 1);

imgFile.SaveAs;

<style type=”text/css”>
#mail{
font-family:”微软雅黑”, “宋体”,arial;
 font-size:12px;
height:530px;
width:662px;
backgroundnull:url() no-repeat
top left;
}
#photo{
height:380px;
width:630px;
position:absolute;
top:20px;
left:25px;
background:url([bgpic]) no-repeat top left;

h2, h3 {
text-align: center;
color: #00B2EE;
}

//定义允许上传的文件扩展名
/*Hashtable extTable = new Hashtable();
extTable.Add(“image”, “gif,jpg,jpeg,png,bmp”);
extTable.Add(“flash”, “swf,flv”);
extTable.Add(“media”,
“swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4”);
extTable.Add(“file”,
“doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4,wps”);*/

//创建文件夹
dirPath += dirName + “/”;
saveUrl += dirName + “/”;
if (!Directory.Exists
{
Directory.CreateDirectory;
}
String ymd = DateTime.Now.ToString(“yyyyMMdd”,
DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + “/”;
saveUrl += ymd + “/”;
if (!Directory.Exists
{
Directory.CreateDirectory;
}

function GetContent($type){
  if( $type )
  {
   if(file_exists(‘./mail_room.html’) )
   {
    $content = file_get_contents( ‘./mail_room.html’);
   }
   else
   {
    ShowMsg(‘file can’ read fail ‘);
   }
  }
  else
  {
   if( file_exists( ‘./mail_person.html’) )
   {
    $content = file_get_contents( ‘./mail_person.html’);
   }
   else
   {
    ShowMsg(‘person file read fail!’);
   }
   
  }
  return $content;
 }
 
 function template($str)
 {
  $_url = $_SERVER[‘HTTP_HOST’];
  $_temp = str_replace(‘{username}’,$_SESSION[‘uname’],$str);
  $_temp = str_replace(‘[bgpic]’,getPic(),$_temp);
  $_temp = str_replace(‘{url}’,$_url,$_temp);
  return $_temp;
 }

  2.body内代码

//if (context.Request.Cookies[“Member”] != null)
//{
// savePath += context.Request.Cookies[“Member”][“MemberId”] +
“/”;
// saveUrl += context.Request.Cookies[“Member”][“MemberId”] + “/”;
//}
string Member = Guid.NewGuid().ToString().Trim().Replace(“-“, “”);
savePath += Member + “/”;
saveUrl += Member + “/”;

图片 1

#lineDiv {
width: 50px;
height: 400px;
float: left;
}

2.上传页面UpFilePage.aspx

StringBuilder strHtml = new StringBuilder();
HttpCookie fileCookie = Request.Cookies[“FileCookie”];
if (fileCookie != null)
{
string[] fileArray = new string[1];
if (fileCookie.Value.Contains
fileArray = fileCookie.Value.Split;
else
fileArray[0] = fileCookie.Value;
foreach (string objFile in fileArray)
{
if (!string.IsNullOrEmpty && objFile.Contains
{
string[] file = objFile.Split;
strHtml.Append(@”<div class=’uploadifyQueueItem’>”);
strHtml.Append(@”<div class=’cancel’>”);
strHtml.Append(“<a href=’javascript:deleteFile(\”” + file[1] +
“\”)’></a>”);
//strHtml.Append(@”<img src=’/Scripts/jquery.uploadify/cancel.png’
border=’0′>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”<span class=’fileName’>” + HttpUtility.UrlDecode

div form {
text-align: center;
vertical-align: middle;
}

StringBuilder strHtml = new StringBuilder();
if (fileCookie.Value.Contains(“|”))
fileArray = fileCookie.Value.Split(‘|’);
else
fileArray[0] = fileCookie.Value;
foreach (string objFile in fileArray)
{
if (!string.IsNullOrEmpty(objFile) && objFile.Contains(“,”))
{
string[] file = objFile.Split(‘,’);
strHtml.Append(@”<div class=’uploadifyQueueItem’>”);
strHtml.Append(@”<div class=’cancel’>”);
strHtml.Append(“<a href=’javascript:deleteFile(\”” + file[1] +
“\”)’></a>”);
//strHtml.Append(@”<img
src=’/Scripts/jquery.uploadify-v2.1.0/cancel.png’ border=’0′>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”<span class=’fileName’>” +
HttpUtility.UrlDecode(file[0]) + “</span><span
class=’percentage’> – 100%</span><div
class=’uploadifyProgress’>”);
strHtml.Append(@”<div class=’uploadifyProgressBar’ style=’width:
100%;’>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”</div>”);
}
}
context.Response.Write(strHtml.ToString());
}
#endregion
}
else if (operate == “GetFile”)
{
#region 获取上传的附件并展示
StringBuilder strHtml = new StringBuilder();
HttpCookie fileCookie = context.Request.Cookies[“FileCookie”];
if (fileCookie != null)
{
string[] fileArray = new string[1];
if (fileCookie.Value.Contains(“|”))
fileArray = fileCookie.Value.Split(‘|’);
else
fileArray[0] = fileCookie.Value;
foreach (string objFile in fileArray)
{
if (!string.IsNullOrEmpty(objFile) && objFile.Contains(“,”))
{
string[] file = objFile.Split(‘,’);
strHtml.Append(@”<div class=’uploadifyQueueItem’>”);
strHtml.Append(@”<div class=’cancel’>”);
strHtml.Append(“<a href=’javascript:deleteFile(\”” + file[1] +
“\”)’>”);
//strHtml.Append(@”<img
src=’/Scripts/jquery.uploadify-v2.1.0/cancel.png’
border=’0′></a>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”<span class=’fileName’>” +
HttpUtility.UrlDecode(file[0]) + “</span><span
class=’percentage’> – 100%</span><div
class=’uploadifyProgress’>”);
strHtml.Append(@”<div class=’uploadifyProgressBar’ style=’width:
100%;’>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”</div>”);
strHtml.Append(@”</div>”);
}
}
}
context.Response.Write(strHtml.ToString());
#endregion
}
}

3.UploadAction文件夹下的一般处理程序:

img {
width: 25px;
height: 25px;
}
.btn {
width: 150px;
height: 40px;
text-align: center;
background-color: #b58061;
color: white;
}
p {
cursor: pointer;
}
</style>
<script
src=”~/Scripts/jquery-1.8.2.min.js”></script>
<script
src=”~/Scripts/uploadify/jquery.uploadify-3.1.js”></script>
<script
type=”text/javascript”>

//创建文件夹
dirPath += dirName + “/”;
saveUrl += dirName + “/”;
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
String ymd = DateTime.Now.ToString(“yyyyMMdd”,
DateTimeFormatInfo.InvariantInfo);
dirPath += ymd + “/”;
saveUrl += ymd + “/”;
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

String fileName = imgFile.FileName;
String fileExt = Path.GetExtension.ToLower();

#imglist {
width: 45%;
height: 400px;
float: left;
}

 

//if (context.Request.Cookies[“Member”] != null)
//{
// savePath += context.Request.Cookies[“Member”][“MemberId”] +
“/”;
// saveUrl += context.Request.Cookies[“Member”][“MemberId”] + “/”;
//}
string Member = Guid.NewGuid().ToString.Replace;
savePath += Member + “/”;
saveUrl += Member + “/”;

//在线打开文件
function openFile {
try {
var fileName = $.text();
var url = window.location.protocol + “//”

关键代码:

//定义允许上传的文件扩展名
/*Hashtable extTable = new Hashtable();
extTable.Add(“image”, “gif,jpg,jpeg,png,bmp”);
extTable.Add(“flash”, “swf,flv”);
extTable.Add(“media”,
“swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4”);
extTable.Add(“file”,
“doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4,wps”);*/