非常简单的例子。ASP.NET例子。。高手百送分的题。。。(50分)

  • 主题发起人 主题发起人 蓝叶菱
  • 开始时间 开始时间

蓝叶菱

Unregistered / Unconfirmed
GUEST, unregistred user!
upload.aspx
---------
<%@ Page Language=&quot;C#&quot;
AutoEventWireup=&quot;true&quot;
CodeFile=&quot;upload.aspx.cs&quot;
Inherits=&quot;upload&quot;
%>
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>
<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
>
<head runat=&quot;server&quot;>
<title>无标题页</title>
</head>
<body>
<form id=&quot;form1&quot;
runat=&quot;server&quot;>
<div style=&quot;text-align: center&quot;>
<strong>UPLOAD SAMPLE<br />
<br />
<input id=&quot;File1&quot;
style=&quot;width: 473px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File2&quot;
style=&quot;width: 471px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File3&quot;
style=&quot;width: 469px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File4&quot;
style=&quot;width: 469px&quot;
type=&quot;file&quot;
/><br />
<asp:LinkButton ID=&quot;LinkButton1&quot;
runat=&quot;server&quot;>LinkButton</asp:LinkButton>

<a href=&quot;JavaScript:document.forms[0].reset()&quot;
id=&quot;LinkButton2&quot;
style=&quot;FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana&quot;>
Reset Form</A> <STRONG>::</STRONG>
<P>

<br />
<asp:Label ID=&quot;Label1&quot;
runat=&quot;server&quot;
Text=&quot;Label&quot;
Width=&quot;440px&quot;></asp:Label>
<asp:Label ID=&quot;Label2&quot;
runat=&quot;server&quot;
Text=&quot;Label&quot;
Width=&quot;188px&quot;></asp:Label>
</strong></div>
</form>
</body>
</html>
--------------
upload.asp.cs
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;
public partial class upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
this.SaveImages();
}
private System.Boolean SaveImages()
{
//loop through the files uploaded
System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
//Message to the user
System.Text.StringBuilder _message = new System.Text.StringBuilder(&quot;Files Uploaded:<br>&quot;);
Label2.Text =System.Convert.ToString( System.Web.HttpContext.Current.Request.Files.Count);
//为什么这里,没有得到input file的数量啊.
try
{
for (System.Int32 _iFile = 0;
_iFile < _files.Count;
_iFile++)
{
// Check to make sure the uploaded file is a jpg or gif
System.Web.HttpPostedFile _postedFile = _files[_iFile];
System.String _fileName, _fileExtension;
_fileName = System.IO.Path.GetFileName(
_postedFile.FileName);
_fileExtension = System.IO.Path.GetExtension(
_fileName);
if (_fileExtension == &quot;.gif&quot;)
{
//Save File to the proper directory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
&quot;gifs/&quot;) + _fileName);
_message.Append(_fileName + &quot;<BR>&quot;);
}
else
if (_fileExtension == &quot;.jpg&quot;)
{
//Save File to the proper directory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
&quot;jpgs/&quot;) + _fileName);
_message.Append(_fileName + &quot;<BR>&quot;);
}
else
{
_message.Append(_fileName + &quot;
<font color=/&quot;red/&quot;>failed!! Only .gif and .jpg images allowed!</font> <BR>&quot;);
}
}
Label1.Text = _message.ToString();
return true;
}
catch (System.Exception Ex)
{
Label1.Text = Ex.Message;
return false;
}
}
}
-------------
为什么request.Fiels取不到任何值。。。。。。。。。。。。。。。。
 
帮忙啊。。。。
 
各位,上面代码,怎么也无法上传图片,我查request.files.count居然是0???!!!!!!
 
我是用这个方法,上传 一切正常。
private void upfile()
{
try
{
string filename = File1.PostedFile.FileName;
string togo = &quot;files/&quot;
+ filename.Remove(0, filename.LastIndexOf(&quot;//&quot;) + 1);
File1.PostedFile.SaveAs(Server.MapPath(togo));
long fileSize = File1.PostedFile.ContentLength;
string fileSizeStr;
if(fileSize > 1000000) fileSizeStr = fileSize/1000000 + &quot;
Mb&quot;;
else
if(fileSize > 1000) fileSizeStr = fileSize/1000 + &quot;
Kb&quot;;
else
fileSizeStr = fileSize + &quot;
b&quot;;

string myfilename=filename.Remove(0, filename.LastIndexOf(&quot;//&quot;) + 1);
OleDbConnection Olecn=new OleDbConnection(Session[&quot;constr&quot;].ToString());
Olecn.Open();
string sql=&quot;insert into files(filename,wjms,updatetime,filelocate) select '&quot;+myfilename+&quot;[&quot;+fileSizeStr +&quot;]&quot;
+&quot;','&quot;+WJMS.Text.Trim()+&quot;',#&quot;+DateTime.Now.ToString()+&quot;#,'files/&quot;+myfilename+&quot;'&quot;;
OleDbCommand cm=new OleDbCommand(sql,Olecn);
cm.ExecuteNonQuery();
Olecn.Close();
Response.Write(&quot;<script>alert('文件上传成功!')</script>&quot;);
}
catch
{
Response.Write(&quot;<script>alert('文件上传失败,可能文件不存在或文件大于4M!')</script>&quot;);
}
}
 
<input id=&quot;File1&quot;
style=&quot;width: 473px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File2&quot;
style=&quot;width: 471px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File3&quot;
style=&quot;width: 469px&quot;
type=&quot;file&quot;
/><br />
<br />
<input id=&quot;File4&quot;
style=&quot;width: 469px&quot;
type=&quot;file&quot;
/><br />
要求File必须使用HTML的组件,不准使用web /ASP.net的控件。。。。
控制以上的。。。
 
朋友你可能误会了,这里的 File1 是 HTML 组件,不是 .net 组件。
 
在这里发一个讨论的问题,如何实现文件的多线程断点续传!!主要是为了针对超大文件的上传来考虑的!!
 
如果大文件修改IIS的配置,就可以了,就可以传输大文件,如果真的要完美传输的话,开发ASP的SERVER组件,参见书籍。

都他妈的垃圾,这个问题还得自己解答,除了楼上的给分外,需要换题目了。
<input id=&quot;File1&quot;
style=&quot;width: 473px&quot;
type=&quot;file&quot;
/><br />
转换成Server组件就可以了。
<input id=&quot;File1&quot;
style=&quot;width: 473px&quot;
type=&quot;file&quot;
runat=&quot;server&quot;
/><br />
 
算了,散分吧。
 
后退
顶部