ASPxUploadControl
/*******************快速使用*************************/
定好保存路径
设置控件的fileuploadcomplete事件
using System.IO;
using DevExpress.Web.ASPxUploadControl;
#region 上传文件
///
/// 保存上传文件
///
///
///
protected string SavePostedFiles(UploadedFile uploadedFile)
{
//保存至服务器上的路径
fangqm.Netbank.Core.Savepath savepath = new fangqm.Netbank.Core.Savepath();
UploadDirectory = savepath.getuploadfileSavePath(DateTime.Now.ToShortDateString());
if (!Directory.Exists(Server.MapPath(UploadDirectory)))
{
Directory.CreateDirectory(Server.MapPath(UploadDirectory));
}
using DevExpress.Web.ASPxUploadControl;
#region 上传文件
///
/// 保存上传文件
///
///
///
protected string SavePostedFiles(UploadedFile uploadedFile)
{
//保存至服务器上的路径
fangqm.Netbank.Core.Savepath savepath = new fangqm.Netbank.Core.Savepath();
UploadDirectory = savepath.getuploadfileSavePath(DateTime.Now.ToShortDateString());
if (!Directory.Exists(Server.MapPath(UploadDirectory)))
{
Directory.CreateDirectory(Server.MapPath(UploadDirectory));
}
string ret = "";
if (uploadedFile.IsValid)
{ //显示进度
FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
string resFileName = MapPath(UploadDirectory) + fileInfo.Name;
uploadedFile.SaveAs(resFileName);
if (uploadedFile.IsValid)
{ //显示进度
FileInfo fileInfo = new FileInfo(uploadedFile.FileName);
string resFileName = MapPath(UploadDirectory) + fileInfo.Name;
uploadedFile.SaveAs(resFileName);
string fileLabel = fileInfo.Name;
string fileType = uploadedFile.PostedFile.ContentType.ToString();
string fileLength = uploadedFile.PostedFile.ContentLength / 1024 + "K";
ret = string.Format("{0} ({1}){2}|{3}", fileLabel, fileType, fileLength, fileInfo.Name);
}
return ret;
}
protected void ASPxUploadControl1_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
{
try
{
e.CallbackData = SavePostedFiles(e.UploadedFile);
}
catch (Exception ex)
{
e.IsValid = false;
e.ErrorText = ex.Message;
}
}
string fileType = uploadedFile.PostedFile.ContentType.ToString();
string fileLength = uploadedFile.PostedFile.ContentLength / 1024 + "K";
ret = string.Format("{0} ({1}){2}|{3}", fileLabel, fileType, fileLength, fileInfo.Name);
}
return ret;
}
protected void ASPxUploadControl1_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
{
try
{
e.CallbackData = SavePostedFiles(e.UploadedFile);
}
catch (Exception ex)
{
e.IsValid = false;
e.ErrorText = ex.Message;
}
}
#endregion
ValidationSettings属性下,可以设置上传文件大小(字节),文件类型,文件扩展名
设置上传文件大小(字节)MaxFileSize="4000000"
文件类型 AllowedContentTypes="image/jpeg,image/gif,image/pjpeg"
文件扩展名 AllowedFileExtensions=".jpg,.jpeg,.jpe,.gif"
设置上传文件大小(字节)MaxFileSize="4000000"
文件类型 AllowedContentTypes="image/jpeg,image/gif,image/pjpeg"
文件扩展名 AllowedFileExtensions=".jpg,.jpeg,.jpe,.gif"
如果使用中发现取不到服务器控件如aspxtextbox值,可以将其FileUploadMode="OnPageLoad"即可
File Type
MIME Content Type
.txt (text document)text/plain.pdf (PDF document)application/pdf.doc (MS Word document)application/msword.docx (MS Word 2007 document)application/vnd.openxmlformats-officedocument.wordprocessingml.document.xls (MS Excel document)application/vnd.ms-excel.xlsx (MS Excel 2007 document)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.jpeg or .jpg (JPEG image)image/jpeg,image/pjpeg.gif (GIF image)image/gif.png (PNG image)image/png,image/x-png.bmp (BMP image)image/bmp.zip (ZIP archive)application/zip
MIME Content Type
.txt (text document)text/plain.pdf (PDF document)application/pdf.doc (MS Word document)application/msword.docx (MS Word 2007 document)application/vnd.openxmlformats-officedocument.wordprocessingml.document.xls (MS Excel document)application/vnd.ms-excel.xlsx (MS Excel 2007 document)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.jpeg or .jpg (JPEG image)image/jpeg,image/pjpeg.gif (GIF image)image/gif.png (PNG image)image/png,image/x-png.bmp (BMP image)image/bmp.zip (ZIP archive)application/zip
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23109131/viewspace-670868/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23109131/viewspace-670868/