62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;//上传文件名字
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upimg") + "\\" + name;
string fpath = Server.MapPath("upfile") + "\\" + name;
string path="F:\\aaa\\"+FileUpload1.FileName;
string wpath = "upimg\\" + name;
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs("F:\\aaa\\"+FileUpload1.FileName);
// Image1.ImageUrl="F:\\aaa\\"+FileUpload1.FileName;
Label1.Text = "你传图片的名字是" + name + "<br>文件大小为" + size + "<br>文件类型为" + type2 + "<br>文件路径为" + ipath;
}
SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("insert into Image(imageName,imagepath) values('" + name + "','" + path + "')", cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{ SqlConnection cn = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");
SqlCommand cmd = new SqlCommand("select imageName from Image where imageID='" + Convert.ToInt32(TextBox1.Text) + "'", cn);
cn.Open();
string a = cmd.ExecuteScalar().ToString();
cn.Close();
Image1.ImageUrl = "F:\\aaa\\" + a;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Boolean fileOk = false;
//获取文件物理路径
string path = Server.MapPath("~/Account/");
//判断是否选择文件
if (FileUpload1.HasFile)
{
//取得文件扩展名
string exName = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
//限定文件类型
string[] allowExtn = { ".jpg", ".txt", ".gif" };
//对上传的文件的类型进行一个个匹对
for (int i = 0; i < allowExtn.Length; i++)
{
if (exName == allowExtn[i])
{
fileOk = true;
break;
}
}
////对上传文件的大小进行检测,限定文件最大不超过1M
//if (FileUpload1.PostedFile.ContentLength > 1024000)
//{
// fileOk = false;
//}
if (!fileOk)
{
Response.Write("<script>alert('文件类型不对');</script>");
}
else
{
try
{
FileUpload1.PostedFile.SaveAs(path + FileUpload1.FileName);
Response.Write("<script>alert('上传成功');</script>");
}
catch
{
Response.Write("<script>alert('上传失败');</script>");
}
}
}
/// <summary>
/// 上传文件到指定的文件夹,返回不包含文件路径
/// </summary>
/// <param name="fileControl">上传文件控件</param>
/// <param name="folderName">文件夹名称</param>
/// <returns></returns>
public static string UploadFile(FileUpload fileControl, string folderName)
{
if (fileControl.HasFile)
{
DateTime dtNow = DateTime.Now;
string filedirect = "/UploadFile/upload/" + folderName + "/";
if (!Directory.Exists(GetMapPath(filedirect)))
{
Directory.CreateDirectory(GetMapPath(filedirect));
}
//File.
string fileName = dtNow.Year.ToString() + dtNow.Month.ToString() + dtNow.Day.ToString() + dtNow.Day.ToString() + dtNow.Hour.ToString() + dtNow.Millisecond.ToString() + "文件扩展名,这个自己去截取";
fileControl.SaveAs(GetMapPath(filedirect + fileName));
return fileName;
}
else
{
return "";
}
}
/// <summary>
/// 获得当前绝对路径
/// </summary>
/// <param name="strPath">指定的路径</param>
/// <returns>绝对路径</returns>
public static string GetMapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}