
Nguyên bản được gửi bởi
NgocTuanz
Mình đang tìm hiểu cách upload nhiều file cùng lúc.
Mình tìm thấy cái uploadify này khá là hay.
Nhưng làm theo hướng dẫn thì ko thể chạy được.
Ai biết cách config hoặc có demo cho mình xin với nhé.
Cảm ơn
nội dung jQuery
Code:
jQuery(document).ready(function () {
$("#file_upload").uploadify({
'swf': 'plugins/uploadify/uploadify.swf',
'buttonText': 'Chọn ảnh từ máy tính của bạn',
'width': 220,
'multi': false,
'fileTypeDesc': 'Tệp hình ảnh',
'fileTypeExts': '*.gif; *.jpg; *.png',
'uploader': 'handlers/FileUpload.ashx',
'onDialogClose': function (queueData) {
if (queueData.filesSelected > 0) {
//code
}
},
'onUploadComplete': function (file) {
//code
},
'onUploadSuccess': function (file, data, response) {
//code
});
}
});
});
nội dung html
Code:
<input type="file" name="file_upload" id="file_upload" />
nôi dung FileUpload.ashx
Code:
using System;
using System.Web;
using System.IO;
using System.Web.SessionState;
public class FileUpload : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest (HttpContext context) {
string path = HttpContext.Current.Request.PhysicalApplicationPath;
HttpPostedFile postedFile = context.Request.Files["Filedata"];
if (context.Request.Files.Count > 0)
{
string pathFile = "";
Boolean fileOK = false;
string fileTypeExts;
string fileName = postedFile.FileName;
int position = fileName.LastIndexOf(".");
string fileExtension = fileName.Substring(position);
fileTypeExts = System.Configuration.ConfigurationManager.AppSettings["ImagesExtensions"];
if (fileTypeExts.IndexOf(fileExtension) > -1)
{
pathFile = System.Configuration.ConfigurationManager.AppSettings["PathImgAvatar"];
fileOK = true;
}
fileTypeExts = System.Configuration.ConfigurationManager.AppSettings["DocumentsExtensions"];
if (fileTypeExts.IndexOf(fileExtension) > -1)
{
pathFile = System.Configuration.ConfigurationManager.AppSettings["PathDocuments"];
fileOK = true;
}
fileTypeExts = System.Configuration.ConfigurationManager.AppSettings["AudiosExtensions"];
if (fileTypeExts.IndexOf(fileExtension) > -1)
{
pathFile = System.Configuration.ConfigurationManager.AppSettings["PathAudios"];
fileOK = true;
}
fileTypeExts = System.Configuration.ConfigurationManager.AppSettings["VideosExtensions"];
if (fileTypeExts.IndexOf(fileExtension) > -1)
{
pathFile = System.Configuration.ConfigurationManager.AppSettings["PathVideos"];
fileOK = true;
}
if (fileOK)
{
string strSaveLocation = path + pathFile;
//postedFile.SaveAs(strSaveLocation);
string filename = postedFile.FileName;
if (!Directory.Exists(strSaveLocation))
Directory.CreateDirectory(strSaveLocation);
postedFile.SaveAs(strSaveLocation + filename);
context.Response.ContentType = "text/plain";
context.Response.Write("<img src=\"/" + pathFile + fileName + "\" width=\"530\" id=\"target\" />");
context.Response.StatusCode = 200;
}
else
{
}
}
}
public bool IsReusable {
get {
return false;
}
}
}