截取字符串,计算字符串字节大小,选择文件夹,上传文件
时间:2010-09-01 来源:裴鹏
获取(.)点前一个字符
string s = "123_u.jpg";
string strKzm = s.Substring(s.LastIndexOf(".")-1);//获取文件扩展名
string strImgNo1 = strKzm.Substring(0, 1);
Response.Write(strImgNo1);
计算字符串字节大小
string str = "ABC";
int s= System.Text.Encoding.GetEncoding("GB2312").GetByteCount(str);
Response.Write(s);
选择文件夹
<script type="text/javascript">
function BrowseFolder(){
try{
var Message = "请选择文件夹"; //选择框提示信息
var Shell = new ActiveXObject( "Shell.Application" );
var Folder = Shell.BrowseForFolder(0,Message,0x0040,0x11);//起始目录为:我的电脑
//var Folder = Shell.BrowseForFolder(0,Message,0); //起始目录为:桌面
if(Folder != null){
Folder = Folder.items(); // 返回 FolderItems 对象
Folder = Folder.item(); // 返回 Folderitem 对象
Folder = Folder.Path; // 返回路径
if(Folder.charAt(Folder.length-1) != "\\"){
Folder = Folder + "\\";
}
document.all.savePath.value=Folder;
return Folder;
}
}catch(e){
alert(e.message);
}
}
</script>
<input type="button" name="chosefolderbutton" value="选择" onclick="BrowseFolder()" />
注意:提示"没有权限"
1、设置可信任站点(如:http://localhost/)
2、其次:可信任站点安全级别自定义设置中:设置 "对没有标记为安全的ActiveX控件进行初始化和脚本运行"----"启用"
上传文件
if (this.FileUpload1.HasFile)//检查是否有文件
{
string fullFileName = this.FileUpload1.PostedFile.FileName;
//string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式
if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp")
{
if (this.FileUpload1.PostedFile.ContentLength > 20 * 1024)
{
Response.Write("<script>alert('上传图片必须小于20k!');</script>");
}
else
{
string path = HttpContext.Current.Request.MapPath("~/zp/");//获取上传文件的网站目录路径
Random rand = new Random();
string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rand.Next(100, 999)+"."+type;
this.FileUpload1.SaveAs(path + fileName);
Response.Write("<script>alert('图片上传成功!');</script>");
this.Image1.ImageUrl = "~/zp/" + fileName;//显示图片
}
}
else
{
Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
}
}
else
{
Response.Write("<script>alert('必须指定文件!');</script>");
}
}