asp.net怎么用上传控件,上传图片!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net怎么用上传控件,上传图片!相关的知识,希望对你有一定的参考价值。

这里的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);//上传文件
参考技术A 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("上传文件格式不正确");


参考技术B 上传
前台代码:
<%@ 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();


参考技术C http://user.qzone.qq.com/156490391/blog/1315445184
我空间里有这篇文章,你可以读一下,希望对你有用。

以上是关于asp.net怎么用上传控件,上传图片!的主要内容,如果未能解决你的问题,请参考以下文章

asp.net如何上传图片

asp.net上传图片

asp.net在线网页编辑器kindeditor怎么上传图片

ASP.NET 用AJAX在页面上传头像,不能异步刷新?

ASP.net 如何实现上传图片的调整和裁减

ASP.NET利用.FileUpload上传图片并将图片名称保存到数据库,我要具体的代码