110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///UploadfileUtil 的摘要说明
/// </summary>
public static class UploadfileUtil
{
public static string CheckFile()
{
//命名一个今天的文件夹
string p = DateTime.Now.ToString("yyyyMMdd");
String folder = System.Web.HttpContext.Current.Server.MapPath("~/uploadpic/images/" + p);
//判断文件是否存在
if (!System.IO.Directory.Exists(folder))
{
//自动生成文件夹
System.IO.Directory.CreateDirectory(folder);
}
return "uploadpic/images/" + p + "/";
}
public static string uploadpic(System.Web.UI.WebControls.FileUpload FileUpload1, int i)
{
string filetype = FileUpload1.PostedFile.ContentType;
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize <= 0)
{
return "0";
}
if (filesize > 1000000)
{
return "00";
}
if (filetype != "image/pjpeg" && filetype != "image/jpeg" && filetype != "image/jpg" && filetype != "image/peg" && filetype != "image/gif" && filetype != "image/x-png" && filetype != "application/x-shockwave-flash")
{
return "000000";
}
string fname = FileUpload1.PostedFile.FileName;
//获取图片的后缀名
if (System.IO.Path.HasExtension(fname))
{
fname = System.IO.Path.GetExtension(fname);
}
else
{
fname = "";
}
//给图片命名
string pic = DateTime.Now.ToString("yyyyMMddHHmmss");
//图片全名组合
fname = pic + fname;
String path1 = CheckFile() + fname;
FileUpload1.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/" + path1));
//返回完整的url
return VirtualPathUtility.ToAbsolute("~/") + path1;
}
}
/// <summary>
/// 检测指定的文件夹是否存在,不存在就创建
/// </summary>
/// <param name="imgpath">该文件夹的之前的路径,注意一定要带上"/"</param>
/// <returns></returns>
public static string CheckFile()
{
//命名一个今天的文件夹
string p = DateTime.Now.ToString("yyyyMMdd");
String folder = System.Web.HttpContext.Current.Server.MapPath("/uploadpic/images/" + p);
//判断文件是否存在
if (!System.IO.Directory.Exists(folder))
{
//自动生成文件夹
System.IO.Directory.CreateDirectory(folder);
//生成后返回文件夹名
return folder;
}
//如果存在,直接返回今天的文件夹名
return "uploadpic/images/" + p ;
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="FileUpload1">上传控件</param>
/// <param name="i">图片ID</param>
/// <returns></returns>
public static string uploadpic(FileUpload FileUpload1, int i)
{
string filetype = FileUpload1.PostedFile.ContentType;
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize <= 0)
{
return "0";
}
if (filesize > 1000000)
{
return "00";
}
if (filetype != "image/pjpeg" && filetype != "image/jpeg" && filetype != "image/jpg" && filetype != "image/peg" && filetype != "image/gif" && filetype != "image/x-png" && filetype != "application/x-shockwave-flash")
{
return "000000";
}
string fname = FileUpload1.PostedFile.FileName;
//获取图片的后缀名
fname = fname.Substring(fname.IndexOf('.'), fname.Length - fname.IndexOf('.'));
//给图片命名
string pic = DateTime.Now.ToString("yyyyMMddHHmmss");
//图片全名组合
fname = pic + ".jpg";
String path1 = CheckFile() + "\\" + fname;
FileUpload1.SaveAs(System.Web.HttpContext.Current.Server.MapPath("/" + path1));
FileUpload1.SaveAs(path1);
return path1;
}