asp.net怎么用上传控件,上传图片! 40
4个回答
展开全部
这里的File是控件的ID
string FileName = File.FileName;//获取上传文件的名称
string Str = FileName.Split('.')[1];//获取上传文件的后缀
string NewName = DateTime.Now.ToString("yyyyMMddhhmmss")+"."+Str;//重命名上传文件
string FilePath = Server.MapPath("~/Image/");//设置上传文件的保存路径
if (!Directory.Exists(FilePath))//判断路径是否存在
{
Directory.CreateDirectory(FilePath);//如果不存在创建文件夹
}
File.SaveAs(FilePath + NewName);//上传文件
string FileName = File.FileName;//获取上传文件的名称
string Str = FileName.Split('.')[1];//获取上传文件的后缀
string NewName = DateTime.Now.ToString("yyyyMMddhhmmss")+"."+Str;//重命名上传文件
string FilePath = Server.MapPath("~/Image/");//设置上传文件的保存路径
if (!Directory.Exists(FilePath))//判断路径是否存在
{
Directory.CreateDirectory(FilePath);//如果不存在创建文件夹
}
File.SaveAs(FilePath + NewName);//上传文件
展开全部
string m_sUpFilePath = Server.MapPath("../" + "UpFiles/UpFile/");
string m_sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
HttpFileCollection m_hfcPro = Request.Files;
HttpPostedFile myFile = m_hfcPro[0];
string savename = m_sFileName + Path.GetExtension(this.downUrl.FileName).ToLower();
m_sFileName = m_sUpFilePath + @"\" + m_sFileName;
if (downUrl.FileName.HasFile )
{
if (this.downUrl.HasFile && (Path.GetExtension(this.downUrl.FileName).ToLower() != ".jpg" || Path.GetExtension(this.downUrl.FileName) != ".gif" || Path.GetExtension(this.downUrl.FileName) != ".png") && Common.Left(this.downUrl.PostedFile.ContentType, 5).ToLower() == "image")
{
SqlConnection m_conAdd = Common.CreateConn();
m_conAdd.Open();
SqlCommand m_comAdd = new SqlCommand("Insert Into weblink(SiteName,LinkUrl,LinkLogo,OrderID,IsCheck,IsMain,Notes)Values(@sitename,@linkurl,@linklogo,@orderid,@ischeck,@ismain,@notes)", m_conAdd);
m_comAdd.Parameters.Add("@sitename", SqlDbType.VarChar, 200).Value = this.siteName.Text;
m_comAdd.Parameters.Add("@linkurl", SqlDbType.VarChar, 100).Value = this.siteLink.Text;
m_comAdd.Parameters.Add("@linklogo", SqlDbType.VarChar, 100).Value = savename;
m_comAdd.Parameters.Add("@orderid", SqlDbType.Int).Value = (this.siteOrder.Text.Trim() != "" ? Convert.ToInt32(this.siteOrder.Text) : 21);
m_comAdd.Parameters.Add("@ischeck", SqlDbType.Int).Value = 0;
m_comAdd.Parameters.Add("@ismain", SqlDbType.Int).Value = 0;
m_comAdd.Parameters.Add("@notes", SqlDbType.VarChar, 1000).Value = Common.CutString(this.siteNotes.Text, 1000);
m_comAdd.ExecuteNonQuery();
m_comAdd.Dispose();
m_conAdd.Close();
m_conAdd.Dispose();
this.downUrl.SaveAs(m_sUpFilePath + savename);
}
else
{
Common.Alert("上传文件格式不正确");
}
}
string m_sFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString();
HttpFileCollection m_hfcPro = Request.Files;
HttpPostedFile myFile = m_hfcPro[0];
string savename = m_sFileName + Path.GetExtension(this.downUrl.FileName).ToLower();
m_sFileName = m_sUpFilePath + @"\" + m_sFileName;
if (downUrl.FileName.HasFile )
{
if (this.downUrl.HasFile && (Path.GetExtension(this.downUrl.FileName).ToLower() != ".jpg" || Path.GetExtension(this.downUrl.FileName) != ".gif" || Path.GetExtension(this.downUrl.FileName) != ".png") && Common.Left(this.downUrl.PostedFile.ContentType, 5).ToLower() == "image")
{
SqlConnection m_conAdd = Common.CreateConn();
m_conAdd.Open();
SqlCommand m_comAdd = new SqlCommand("Insert Into weblink(SiteName,LinkUrl,LinkLogo,OrderID,IsCheck,IsMain,Notes)Values(@sitename,@linkurl,@linklogo,@orderid,@ischeck,@ismain,@notes)", m_conAdd);
m_comAdd.Parameters.Add("@sitename", SqlDbType.VarChar, 200).Value = this.siteName.Text;
m_comAdd.Parameters.Add("@linkurl", SqlDbType.VarChar, 100).Value = this.siteLink.Text;
m_comAdd.Parameters.Add("@linklogo", SqlDbType.VarChar, 100).Value = savename;
m_comAdd.Parameters.Add("@orderid", SqlDbType.Int).Value = (this.siteOrder.Text.Trim() != "" ? Convert.ToInt32(this.siteOrder.Text) : 21);
m_comAdd.Parameters.Add("@ischeck", SqlDbType.Int).Value = 0;
m_comAdd.Parameters.Add("@ismain", SqlDbType.Int).Value = 0;
m_comAdd.Parameters.Add("@notes", SqlDbType.VarChar, 1000).Value = Common.CutString(this.siteNotes.Text, 1000);
m_comAdd.ExecuteNonQuery();
m_comAdd.Dispose();
m_conAdd.Close();
m_conAdd.Dispose();
this.downUrl.SaveAs(m_sUpFilePath + savename);
}
else
{
Common.Alert("上传文件格式不正确");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
上传
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="Mypractice.Upload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />
</form>
</body>
</html>
】
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.Windows.Forms;
namespace Mypractice
{
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void UpFile()
{
string FilePath = Server.MapPath("./") + "Newupload";
//Newupload文件夹是根目录下的文件夹
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile hf = HFC[i];
if (HFC[i].ContentLength > 0)
{
hf.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(hf.FileName));
Response.Write("<script>alert('上传成功!')</script>");
MessageBox.Show(hf.FileName);
}
if(hf.FileName=="")
{
MessageBox.Show("aa");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UpFile();
}
}
}
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="Mypractice.Upload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传" />
</form>
</body>
</html>
】
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.Windows.Forms;
namespace Mypractice
{
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void UpFile()
{
string FilePath = Server.MapPath("./") + "Newupload";
//Newupload文件夹是根目录下的文件夹
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile hf = HFC[i];
if (HFC[i].ContentLength > 0)
{
hf.SaveAs(FilePath + "//" + System.IO.Path.GetFileName(hf.FileName));
Response.Write("<script>alert('上传成功!')</script>");
MessageBox.Show(hf.FileName);
}
if(hf.FileName=="")
{
MessageBox.Show("aa");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UpFile();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
http://user.qzone.qq.com/156490391/blog/1315445184
我空间里有这篇文章,你可以读一下,希望对你有用。
我空间里有这篇文章,你可以读一下,希望对你有用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询