function PreviewPhotoatwidth(photo_file,img_object,imgwidth) { debugger; var fileext=photo_file.value.substring(photo_file.value.lastIndexOf("."),photo_file.value.length); fileext=fileext.toLowerCase(); if ((fileext!='.jpg')&&(fileext!='.gif')&&(fileext!='.jpeg')&&(fileext!='.png')&&(fileext!='.bmp'))
</body> </html>using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO;public partial class myPage : System.Web.UI.Page { private void DownloadFile() { string path = Server.MapPath("UploadFiles/07de2121-c906-45e1-8aff-2ce320c12404.jpg"); //string path = this.ApplicationRootPath + objNetDisk_Resource.ResURL; //在你的项目里需要建一个 UploadFiles文件夹 if (!File.Exists(path)) { // 文件没有找到 //Page.Response.Write ("文件不存在") ;
// return; } else { System.IO.FileInfo file = new System.IO.FileInfo(path); // clear the current output content from the buffer Response.Clear(); // add the header that specifies the default filename for the Download/SaveAs dialog Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8)); // add the header that specifies the file size, so that the browser // can show the download progress Response.AddHeader("Content-Length", file.Length.ToString()); // specify that the response is a stream that cannot be read by the // client and must be downloaded Response.ContentType = "application/octet-stream"; // send the file stream to the client Response.WriteFile(file.FullName); // stop the execution of this page Response.End(); } } protected void Page_Load(object sender, EventArgs e) {
这个到底怎么写啊
你的需求浏览器做不到,自己写ActiveX吧。
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>院内展示</title>
<link href="../css/style.css" type="text/css" rel="stylesheet" /> <script language="javascript" type="text/javascript">
function PreviewPhotoatwidth(photo_file,img_object,imgwidth)
{
debugger;
var fileext=photo_file.value.substring(photo_file.value.lastIndexOf("."),photo_file.value.length);
fileext=fileext.toLowerCase();
if ((fileext!='.jpg')&&(fileext!='.gif')&&(fileext!='.jpeg')&&(fileext!='.png')&&(fileext!='.bmp'))
{
alert("对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !");
photo_file.focus();
}
else
{
img_object.src=photo_file.value;
if (img_object.width>imgwidth)
{
img_object.width=imgwidth;
}
}
}
</script></head>
<body >
<form id="form1" runat="server" enctype="multipart/form-data" method="post" action="">
<table width="100%">
<tr>
<td style="font-size: 11px;">
上传图片:
</td>
<td class="contenttext">
<input id="ImageFile" type="file" name="ImageFile" runat="server" onchange="javascript:PreviewPhotoatwidth(this,picpreview,800)"
style="width: 308px" />
<asp:Label ID="ImageFileLabel" runat="server" ForeColor="Red"></asp:Label>
</td>
<td style="font-size: 11px;">
图片预览:
</td>
<td>
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="下载" OnClick="Button2_Click" />
</td>
</tr>
</table>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" />
<img id="picpreview" alt="预览" src="" /></form>
</body>
</html>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;public partial class myPage : System.Web.UI.Page
{
private void DownloadFile()
{
string path = Server.MapPath("UploadFiles/07de2121-c906-45e1-8aff-2ce320c12404.jpg");
//string path = this.ApplicationRootPath + objNetDisk_Resource.ResURL;
//在你的项目里需要建一个 UploadFiles文件夹 if (!File.Exists(path))
{
// 文件没有找到
//Page.Response.Write ("文件不存在") ;
// return;
}
else
{
System.IO.FileInfo file = new System.IO.FileInfo(path); // clear the current output content from the buffer
Response.Clear();
// add the header that specifies the default filename for the Download/SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded
Response.ContentType = "application/octet-stream";
// send the file stream to the client
Response.WriteFile(file.FullName);
// stop the execution of this page
Response.End();
}
} protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DateTime CreateDate = DateTime.Now;
string TrueName = "";
string RandomName = ""; if (ImageFile.PostedFile.FileName.Length > 0 )
{
if (ImageFile.PostedFile.FileName != "")
{
string RandomString = Convert.ToString(Guid.NewGuid());
TrueName = Path.GetFileName(ImageFile.PostedFile.FileName);
int i = TrueName.LastIndexOf(".");
string ExtendName = TrueName.Substring(i);
RandomName = RandomString + ExtendName;
string filepath = Server.MapPath("~/UploadFiles/" + RandomName);
//在你的项目里需要建一个 UploadFiles文件夹 //string FileType = UploadFile.PostedFile.ContentType.ToString();
//UploadFileName.EndsWith(".exe")
if (ExtendName != ".jpg" && ExtendName != ".gif" && ExtendName != ".bmp" && ExtendName != ".JPG" && ExtendName != ".GIF" && ExtendName != ".BMP")
{
//Response.Write("<script language=\"javascript\">alert('只能上传jpg,gif,bmp格式的图片!');</script>");
ImageFileLabel.Visible = true;
ImageFileLabel.Text = "只能上传jpg,gif,bmp格式的图片!";
return;
}
if (ImageFile.PostedFile.ContentLength > 20280000)
{
//Response.Write("<script language=\"javascript\">alert('上传图片不能超过200k!');</script>");
ImageFileLabel.Visible = true;
ImageFileLabel.Text = "上传图片不能超过200k";
return;
}
else
{
ImageFile.PostedFile.SaveAs(filepath);
}
}
else
{
ImageFileLabel.Visible = true;
ImageFileLabel.Text = "必须选择图片";
return;
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.DownloadFile();
}
protected void Button3_Click(object sender, EventArgs e)
{
Page.Response.Clear();
string path = Server.MapPath("UploadFiles/07de2121-c906-45e1-8aff-2ce320c12404.jpg");
bool success = Class1.ResponseFile(Page.Request, Page.Response, "1111111111111111", path, 1024000);
if (!success)
Response.Write("下载文件出错!");
Page.Response.End();
}
}
其它的都好办
浏览部分不使用fileupload的话,我刚刚上网查了一下,好像没有找到替代的解决办法
我可以把浏览部分用CSSS搞成最小 用fileupload 该怎么写呢 ?
<%@ Page Language="C#" EnableViewState="true" %><%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
string strCnn = "Persist Security Info=False;User ID=sa;Password=;Initial Catalog=Book;Server=(local);";
protected void Button1_Click( object sender, EventArgs e )
{
System.IO.Stream fileDataStream = FileUpload1.PostedFile.InputStream; if (fileDataStream.Length < 1)
{
Msg.Text = "请选择文件。";
return;
} //得到文件大小
int fileLength = FileUpload1.PostedFile.ContentLength; //创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);
//得到文件类型
string fileType = FileUpload1.PostedFile.ContentType; //构建数据库连接,SQL语句,创建参数 SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand("INSERT INTO UserPhoto (UserName,ContentType,Photo)" +
"VALUES (@UserName,@ContentType,@Photo)", myConnection); command.Parameters.AddWithValue("@UserName", TextBox1.Text);
command.Parameters.AddWithValue("@ContentType", fileType);
command.Parameters.AddWithValue("@Photo", fileData); //打开连接,执行查询
myConnection.Open();
command.ExecuteNonQuery();
myConnection.Close();
Response.Redirect(Request.RawUrl);
}
protected void Page_Load( object sender, EventArgs e )
{ if (!Page.IsPostBack)
{
BindGrid();
}
} private void BindGrid( )
{
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand myCommand = new SqlCommand("SELECT * FROM UserPhoto Order By id DESC", myConnection); try
{
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
GridView1.DataBind();
}
catch (Exception SQLexc)
{
Response.Write("提取数据时出现错误:" + SQLexc.ToString());
}
}
protected string FormatURL( object strArgument )
{
return "ReadImage.aspx?id=" + strArgument.ToString();
} </script><html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>上传文件到数据库</title>
</head>
<body>
<form id="MengXianhui" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%#Eval("UserName") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<img src="<%#FormatURL(Eval("id")) %>" /></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
姓名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
照片:<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="Button1_Click"></asp:Button>
<p>
<asp:Label ID="Msg" runat="server" ForeColor="Red"></asp:Label></p>
</form>
</body>
</html>显示图片
<%@ Page Language="C#" %><%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server"> protected void Page_Load( object sender, EventArgs e )
{
////构建数据库连接,SQL语句,创建参数
//ACCESS数据库使用本注释部分
//string strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Image2Access.mdb");
//OleDbConnection myConnection = new OleDbConnection(strCnn);
//OleDbCommand command = new OleDbCommand("select * from Person Where PersonID =" + Request.QueryString["id"], myConnection);
//myConnection.Open();
//OleDbDataReader dr = command.ExecuteReader();
//if (dr.Read())
//{
// Response.Clear();
// Response.AddHeader("Content-Type", dr["PersonImageType"].ToString());
// Response.BinaryWrite((byte[])dr["PersonImage"]);
//}
//dr.Close();
//myConnection.Dispose();
MSProject开源的ASP.NET论坛V1.3.0
remex 著于2007-7-28 11:04:06
MSProject论坛起源于国际最著名的开源论坛YetAnotherForum,对其的安装步骤、各功能模块及管理界面的进行汉化,并且改进了不少的功能及BUG。其中的在线编辑器里有图片上传的代码
好像是这么写 忘记了 。 自己 看下