这是 .cs:using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;namespace CsdnTest { /// <summary> /// Summary description for FileUploadSimple. /// </summary> public class FileUploadSimple : System.Web.UI.Page { protected System.Web.UI.WebControls.Button btnUpload; protected System.Web.UI.HtmlControls.HtmlInputFile upload;
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); }
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void btnUpload_Click(object sender, System.EventArgs e) { upload.PostedFile.SaveAs(Server.MapPath(".")+"\\theUploadedFile.txt"); Response.Write("Upload successful."); } } }
以下是保存附件的用户控件代码 '保存附件 Public Function SaveDoc(ByVal Sourceid As Integer, ByVal TypeId As Integer) Try Dim documents As New ASPNetPortal.CommonDocument() Dim Fileobj As System.Web.UI.HtmlControls.HtmlInputFile Fileobj = CType(FindControl("FileUpload"), System.Web.UI.HtmlControls.HtmlInputFile) If Not (FileUpload.PostedFile Is Nothing) Then If Not FileUpload.PostedFile.FileName.Trim().Length = 0 Then Dim phyiscalPath As String Dim virtualPath As String = "~/uploads/" & Path.GetFileName(FileUpload.PostedFile.FileName) phyiscalPath = Server.MapPath(virtualPath) FileUpload.PostedFile.SaveAs(phyiscalPath) documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(FileUpload.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, FileUpload.PostedFile.ContentLength, "") End If End If If Not (File1.PostedFile Is Nothing) Then If (File1.PostedFile.FileName <> "") Then If Not File1.PostedFile.FileName.Trim().Length = 0 Then Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File1.PostedFile.FileName) Dim phyiscalPath As String = Server.MapPath(virtualPath) File1.PostedFile.SaveAs(phyiscalPath) documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File1.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File1.PostedFile.ContentLength, "") End If End If End If If Not (File2.PostedFile Is Nothing) Then If (File2.PostedFile.FileName <> "") Then If Not File2.PostedFile.FileName.Trim().Length = 0 Then Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File2.PostedFile.FileName) Dim phyiscalPath As String = Server.MapPath(virtualPath) File2.PostedFile.SaveAs(phyiscalPath) documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File2.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File2.PostedFile.ContentLength, "") End If End If End If If Not (File3.PostedFile Is Nothing) Then If (File3.PostedFile.FileName <> "") Then If Not File3.PostedFile.FileName.Trim().Length = 0 Then Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File3.PostedFile.FileName) Dim phyiscalPath As String = Server.MapPath(virtualPath) File3.PostedFile.SaveAs(phyiscalPath) documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File3.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File3.PostedFile.ContentLength, "") End If End If End If If Not (File4.PostedFile Is Nothing) Then If (File4.PostedFile.FileName <> "") Then If Not File4.PostedFile.FileName.Trim().Length = 0 Then Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File4.PostedFile.FileName) Dim phyiscalPath As String = Server.MapPath(virtualPath) File4.PostedFile.SaveAs(phyiscalPath) documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File4.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File4.PostedFile.ContentLength, "") End If End If End If Catch err As Exception Throw New Exception("出现错误", err) End Try End Function
我的不仅仅为空,而且还报错。
呵呵,咱两看来均为asp.net菜鸟
Dim lstfilename As String
lstfilename = picFile.PostedFile.FileName就这么简单的代码都出错.HTML显示如下:
<form id="Form1" method="post" encType=" multipart/form-data " runat="server">
<INPUT id="picFile" name="picfile" style="Z-INDEX: 123; LEFT: 408px; WIDTH: 259px; POSITION: absolute; TOP: 95px; HEIGHT: 22px" type="file" size="24" runat="server" width="30">
</form>报错为:
server error请教各位大哥,这是为什么啊?拜托了
<FONT face="宋体"><INPUT id="myfile" style="Z-INDEX: 101; LEFT: 130px; POSITION: absolute; TOP: 150px" type="file" runat="server">
<asp:textbox id="TextBox1" style="Z-INDEX: 104; LEFT: 131px; POSITION: absolute; TOP: 179px" runat="server"></asp:textbox>
<asp:button id="Button1" style="Z-INDEX: 103; LEFT: 366px; POSITION: absolute; TOP: 147px" runat="server" Text="up" Width="64px"></asp:button>
<DIV id="myDiv" style="Z-INDEX: 102; LEFT: 128px; WIDTH: 289px; POSITION: absolute; TOP: 54px; HEIGHT: 92px" runat="server" ms_positioning="FlowLayout">
<P><asp:label id="Label2" runat="server" Width="191px">Label</asp:label></P>
<P><asp:label id="Label1" runat="server" Width="181px">Label</asp:label></P>
<P>
<asp:Label id="Label3" runat="server" Width="169px">Label</asp:Label></P>
</DIV>
</FONT>
</form>
webform1.aspx.cs:private void Button1_Click(object sender, System.EventArgs e) {
if (myfile.PostedFile == null || myfile.PostedFile.ContentLength == 0){
this.Label1.Text = "null";
return;
}
else{
Label1.Text = myfile.PostedFile.FileName;
Label2.Text = myfile.PostedFile.ContentLength.ToString();
Label3.Text = myfile.PostedFile.ContentType; string[] fileSplit = myfile.PostedFile.FileName.Split('\\');
string fileName = fileSplit[fileSplit.Length -1];
myfile.PostedFile.SaveAs(Server.MapPath(fileName));
this.Label1.Text += " " + this.TextBox1.Text;
}
}
encType=" multipart/form-data "改为:encType="multipart/form-data"中间不能有空格
给你一个简单的例子,已经测试成功,你先把代码粘回去看看可以不可以,可以的话,再对照看看你的代码是什么问题这是aspx:<%@ Page language="c#" Codebehind="FileUploadSimple.aspx.cs" AutoEventWireup="false" Inherits="CsdnTest.FileUploadSimple" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>FileUploadSimple</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="FileUploadSimple" method="post" runat="server" enctype="multipart/form-data">
<input type="file" runat="server" id="upload">
<br>
<asp:Button ID="btnUpload" Runat="server" Text="Upload"></asp:Button>
</form>
</body>
</HTML>
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace CsdnTest
{
/// <summary>
/// Summary description for FileUploadSimple.
/// </summary>
public class FileUploadSimple : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnUpload;
protected System.Web.UI.HtmlControls.HtmlInputFile upload;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnUpload_Click(object sender, System.EventArgs e)
{
upload.PostedFile.SaveAs(Server.MapPath(".")+"\\theUploadedFile.txt");
Response.Write("Upload successful.");
}
}
}
谢谢tim哥。来个kiss。:)
private void btnUpload_Click(object sender, System.EventArgs e)
{
upload.PostedFile.SaveAs(Server.MapPath(".")+"\\theUploadedFile.txt");
Response.Write("Upload successful.");
}我的PostedFile是为空。。
不过还是谢谢你:)
呵呵,我也与你一样,下次我取个名字“自恋成狂”
(因为爱别人通常没结果,还是爱自己吧:))
'保存附件
Public Function SaveDoc(ByVal Sourceid As Integer, ByVal TypeId As Integer)
Try Dim documents As New ASPNetPortal.CommonDocument()
Dim Fileobj As System.Web.UI.HtmlControls.HtmlInputFile
Fileobj = CType(FindControl("FileUpload"), System.Web.UI.HtmlControls.HtmlInputFile)
If Not (FileUpload.PostedFile Is Nothing) Then
If Not FileUpload.PostedFile.FileName.Trim().Length = 0 Then
Dim phyiscalPath As String
Dim virtualPath As String = "~/uploads/" & Path.GetFileName(FileUpload.PostedFile.FileName)
phyiscalPath = Server.MapPath(virtualPath)
FileUpload.PostedFile.SaveAs(phyiscalPath)
documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(FileUpload.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, FileUpload.PostedFile.ContentLength, "")
End If
End If If Not (File1.PostedFile Is Nothing) Then
If (File1.PostedFile.FileName <> "") Then
If Not File1.PostedFile.FileName.Trim().Length = 0 Then
Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File1.PostedFile.FileName)
Dim phyiscalPath As String = Server.MapPath(virtualPath)
File1.PostedFile.SaveAs(phyiscalPath)
documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File1.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File1.PostedFile.ContentLength, "")
End If
End If
End If If Not (File2.PostedFile Is Nothing) Then
If (File2.PostedFile.FileName <> "") Then
If Not File2.PostedFile.FileName.Trim().Length = 0 Then
Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File2.PostedFile.FileName)
Dim phyiscalPath As String = Server.MapPath(virtualPath)
File2.PostedFile.SaveAs(phyiscalPath)
documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File2.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File2.PostedFile.ContentLength, "")
End If
End If
End If
If Not (File3.PostedFile Is Nothing) Then
If (File3.PostedFile.FileName <> "") Then
If Not File3.PostedFile.FileName.Trim().Length = 0 Then
Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File3.PostedFile.FileName)
Dim phyiscalPath As String = Server.MapPath(virtualPath)
File3.PostedFile.SaveAs(phyiscalPath)
documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File3.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File3.PostedFile.ContentLength, "")
End If
End If
End If If Not (File4.PostedFile Is Nothing) Then
If (File4.PostedFile.FileName <> "") Then
If Not File4.PostedFile.FileName.Trim().Length = 0 Then
Dim virtualPath As String = "~/uploads/" + Path.GetFileName(File4.PostedFile.FileName)
Dim phyiscalPath As String = Server.MapPath(virtualPath)
File4.PostedFile.SaveAs(phyiscalPath)
documents.UpdateDocument(Sourceid, TypeId, Sourceid, Context.User.Identity.Name, Path.GetFileName(File4.PostedFile.FileName), phyiscalPath, "", New Byte(0) {}, File4.PostedFile.ContentLength, "")
End If
End If
End If
Catch err As Exception
Throw New Exception("出现错误", err)
End Try End Function
Fileobj = CType(FindControl("FileUpload"), System.Web.UI.HtmlControls.HtmlInputFile)
这样查出来的Fileobj是不是为空