大伟,补分啦!

kafanqd 2011-10-27 09:32:01
还得麻烦你一件事

asp.net 图片上传怎么实现?

要代码!

我以前是有winform实现的上传,现在想用asp.net的网页实现,谢谢!
...全文
83 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
暖枫无敌 2011-10-27
  • 打赏
  • 举报
回复
图片存储:通过文件上传获取图片并转换成Byte[]字节数组,保存到数据库Image字段
昨晚给你整的读取,我以为你会图片存入数据库呢,下面的根据你需要进行改改,就是数据插入那块,根据你实际字段来。


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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" style="font-size: 12px;" enctype="multipart/form-data">
备 注:<asp:TextBox ID="markname" runat="server"></asp:TextBox>
上 传:<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClientClick="return checkClint()" Text="上 传"
OnClick="Button1_Click" />
<script language="javascript" type="text/javascript">
function checkClint() {
var getmarkname = document.getElementById("markname");
var getfile = document.getElementById("FileUpload1");

if (getmarkname.value == "") {
alert('请输入图片备注名称!');
getmarkname.focus();
return false;
} else if (getfile.value == "") {
alert('请选择上传文件路径!');
getfile.focus();
return false;
} else {
return true;
}
}
</script>
</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.IO;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
//获取数据
string getname = this.markname.Text;
string getfile = this.FileUpload1.PostedFile.FileName;

//上传文件
string getlastpath = FileUploadCompant(this.FileUpload1);

//获取上传文件流
byte[] getbyte = new byte[this.FileUpload1.PostedFile.ContentLength];
Stream filestream = this.FileUpload1.PostedFile.InputStream;

//读入数据
filestream.Read(getbyte, 0, this.FileUpload1.PostedFile.ContentLength);

//插入数据
#region
string sql = "insert into employee(markname,markContent,markType,markSize,markLinkUrl) values(@name,@content,@type,@size,@link)";

SqlParameter[] getpars = new SqlParameter[5];
getpars[0] = new SqlParameter("@name", getname);
getpars[1] = new SqlParameter("@content", getbyte);//文件内容插入This.Fileupload1.FileBytes同样可以直接转换成Byte数组不用转换
getpars[2] = new SqlParameter("@type", this.FileUpload1.PostedFile.ContentType);//保存文件类型
getpars[3] = new SqlParameter("@size", this.FileUpload1.PostedFile.ContentLength);//文件长度
getpars[4] = new SqlParameter("@link", getlastpath);

int getrescount = DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.connString, CommandType.Text, sql, getpars);

if (getrescount == 1)
{
//添加成功
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录成功添加到数据库'); ", true);
}
else
{
//添加失败
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '图片记录添加失败'); ", true);
}

#endregion
}

/// <summary>
/// 文件上传操作-单独隔离出来方法
/// </summary>
public string FileUploadCompant(FileUpload getfileupload)
{
string takeServerpath = string.Empty;//保存到服务器路径

try
{
if (string.IsNullOrEmpty(getfileupload.PostedFile.FileName))
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '请选择上传文件的路径!'); ", true);
getfileupload.Focus();
}
else
{
string filepath = getfileupload.PostedFile.FileName;//获得全路径
//获得文件名
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);

//保存服务器路径 -唯一文件名
string serverpath = Server.MapPath("FileuploadDict/") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss") + Session.SessionID + filename;

//保存到服务器上
getfileupload.SaveAs(serverpath);

//保存成功 提示信息
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传成功!'); ", true);
takeServerpath = serverpath;
}
}
catch (Exception se)
{
//抓取一场信息 并提示
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aler ", "alert( '文件上传失败-上传过程出现如下异常信息:" + se.Message.ToString() + "!'); ", true);
}

return takeServerpath;
}
}

一起得瑟 2011-10-27
  • 打赏
  • 举报
回复
FileUpload 控件 <asp:FileUpload ID="fileHeader" runat="server" />



cs代码:string path = Server.MapPath("~/plmnhytf12f3");
string header = "";
if (fileHeader.FileName != "")
{

path = path + "/jobs/" + id + "/";
header = "/plmnhytf12f3/jobs/" + id + "/" + fileHeader.FileName;
string sql = "update vogues set header='" + header + "' where id=" + id;
DbHelperSQL.GetSingle(sql);

Directory.CreateDirectory(path);
fileHeader.SaveAs(path+fileHeader.FileName);
}


自己看着整呗
电脑染上这个可恶的病毒很长时间了,一直懒得弄。主要是除了每次插U盘都会在U盘下生成隐藏的RECYCLER文件夹和autorun.inf之外,好像并没有危害过我别的东西。可每次拿我的U盘去别人电脑那里总被告知有毒,实在不爽,于是下决心干掉它! Google了一下,试了好几种办法都没有效果(有的方法还很麻烦),下了n个u盘杀毒程序也不都管用。最后在德国杀毒软件小红伞的官方论坛找到了解决办法(小红伞其实根本就对这个病毒毫无反应-_-!!!)。 废话少说,你先看看你中的毒是不是和我一样,一样的话按这个办法去解决,保证简单有效。 病毒描述: 1. U盘根目录下生成了一个antorun.inf,还有一个文件夹RECYCLER,病毒被放在I:RECYCLERS-5-3-42-2819952290-8240758988-879315005-3665jwgkvsq.vmx 2. autorun.inf和RECYCLER都可以手动删除,但重新拔插U盘,发现又有了 autorun.inf 和 RECYCLER 文件夹 解决办法: 1. 点击这里下载Symantec为这病毒研发的专杀工具,FixDownadup.exe; 2. 断掉网络,关闭全部程序;关闭系统还原; 3. 执行刚才下载的FixDownadup.exe; 4. 重启电脑;再执行FixDownadup.exe 以确保病毒完全清除; 5. 开启系统还原;链接网络;点击这里,寻找并安装微软系统安全丁(KB958644) 6、打开【运行】对话框(Windows徽标键+R),输入CMD 7、输入cd /d G:\ 。其中G:\为我的存储卡 输入 md autorun.inf\免疫..\ 8、输入md RECYCLER\S-5-3-42-2819952290-8240758988-879315005-3665\jwgkvsq.vmx\免疫..\ 免疫成功,大功告成!有任何问题欢迎在此页CSDN留言。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧