非常着急!!!获取上传文件路径问题 困扰好几天了,怎么都取不到值,查了很多资料,比如FORM标记中加enctype="multipart/form-data",然后也用了很多第三方上传控件,也都取不到值,非常着急!!!难道会跟机器的什么权限有关?非常着急!!!!!!!!!!能帮我解决了再加多少分都可以。客户等不及了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么不用asp.net里的upload控件 兄弟们,我现在是非常着急我用的是HTMLINPUT控件,它的file1.PostedFile.FileName;老是说没实例化,我查了好几天了,网上一般说的解决方法我都试过了,还是照样出那个错……高手快来帮兄弟一把!!! 在FORM中如果有runat=server,就不用enctype="multipart/form-data 在工具箱html中添加File Field控件,然后单击右键作为服务器控件运行。在C:\Inetpub\wwwroot\upLoad(应用程序在iis中的目录)目录下创建一个upLoad的文件夹(名字可以变化)下面是具体的编程VB语法:Dim FullFileName As String = Me.UpLoadFile.PostedFile.FileNameDim FileName As String = Me.UpLoadFile.PostedFile.FileName.Substring(FullFileName.LastIndexOf("\") + 1) Dim FileType As String = Me.UpLoadFile.PostedFile.FileName.Substring(FullFileName.LastIndexOf(".") + 1) UpLoadFile.PostedFile.SaveAs(Server.MapPath("upLoad") & "\" & FileName) C#语法 string fullfileName=this.file1.postedFile.fileNmae; string fileName=this.File1.PostedFile.FileName.Substring(fullFileName.LastIndexOf("\\")+1); string type= fullFillName.Substing(fullFileName.LastIndexOf("."); File1.PostedFile.SaveAs(server.MapPath("upLoad")+"\\"+fileName);如果有问题请与qq:407775797联系 是不是以前已经用过HTMLINPUT控件,被删除后后台定义的没删完.建议在老项目里面新建一页面测试. 给你个多文件上传的例子:<%@ Page language="c#" Codebehind="upload.aspx.cs" AutoEventWireup="false" Inherits="test.upload" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>upload</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="javascript"> function addFile() { var str = '<input type="file" size="50" name="File">' document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) } </script> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server" enctype="multipart/form-data"> <div align="center"> <h3>多文件上传</h3> <p id="MyFile"><input type="file" size="50" name="file"></p> <p> <input type="button" value="增加(Add)" onclick="addFile()"> <input type="button" value="重置(ReSet)" onclick="this.form.reset()"> <asp:Button Runat="server" Text="上传" ID="UploadButton"></asp:Button> </p> <p> <asp:Label ID="strStatus" Runat="server" Font-Name="宋体" Font-Bold="true" Font-Size="9pt" Width="500px" BorderStyle="None" BackColor="white"></asp:Label> </p> </div> </form> </body></HTML>.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 test{ /// <summary> /// upload 的摘要说明。 /// </summary> public class upload : System.Web.UI.Page { protected System.Web.UI.WebControls.Button UploadButton; protected System.Web.UI.WebControls.Label strStatus; private void Page_Load(object sender, System.EventArgs e) { if (this.IsPostBack) this.SaveImages(); // 在此处放置用户代码以初始化页面 } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion private Boolean SaveImages() { ///'遍历File表单元素 HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息 System.Text.StringBuilder strMsg = new System.Text.StringBuilder(); strMsg.Append("上传的文件分别是:<hr color=red>"); try { for(int iFile = 0; iFile < files.Count; iFile++) { ///'检查文件扩展名字 HttpPostedFile postedFile = files[iFile]; string fileName, fileExtension; fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName != "") { fileExtension = System.IO.Path.GetExtension(fileName); strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString()+ "<br>"); strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>"); strMsg.Append("上传文件的文件名:" + fileName + "<br>"); strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>"); ///可根据扩展名字的不同保存到不同的文件夹 ///注意:可能要修改你的文件夹的匿名写入权限。 postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName); } // if (fileName != "") // { // fileExtension = System.IO.Path.GetExtension(fileName); // strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString()+".rar" + "<br>"); // strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>"); // strMsg.Append("上传文件的文件名:" + fileName + "<br>"); // strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>"); // ///可根据扩展名字的不同保存到不同的文件夹 // ///注意:可能要修改你的文件夹的匿名写入权限。// postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/ss/") + fileName);// // } // } strStatus.Text = strMsg.ToString(); return true; } catch(System.Exception Ex) { strStatus.Text = Ex.Message; return false; } } }} private void cmd_Upload_Click(object sender, System.EventArgs e) { if(File_Files.PostedFile.FileName != "") { string strAttachName = File_Files.PostedFile.FileName.Substring (File_Piture.PostedFile.FileName.LastIndexOf(".")); string Path = Server.MapPath("../Image/MenuItem/") + System.Guid.NewGuid() + strAttachName; File_Files.PostedFile.SaveAs(Path); img_PackagePic.ImageUrl = Path.Replace(Server.MapPat(@"..\..\") , "http://" + Request.Url.Host + "/").Replace("\\" , "/"); }}看看还行啊! 关于动态调用的一个问题 请问DataGrid中的Textbox,如何能设置其内容? 大家进来帮个忙,有进有分,100分献上。 从ASP初入.NET,发现无从下手,问个数据操作的小白问题 GridView设置了AllowPaging为True后为何OnRowDataBound中有问题 ToolBar如何设置下拉菜单呢? 大家看看这个权限设置的页面怎样实现好 如何能在加载页调用usercontrol的方法、属性 C#实现:如何把一个Word的内容复制到另外一个Word文档中? 扩展Html.DropDownList出现的问题?? 请问哪里有好的人才网卖? 如何把这段代码里的单引号替换成别的什么
我用的是HTMLINPUT控件,它的file1.PostedFile.FileName;老是说没实例化,我查了好几天了,网上一般说的解决方法我都试过了,还是照样出那个错……高手快来帮兄弟一把!!!
在C:\Inetpub\wwwroot\upLoad(应用程序在iis中的目录)目录下创建一个upLoad的文件夹(名字可以变化)
下面是具体的编程
VB语法:
Dim FullFileName As String = Me.UpLoadFile.PostedFile.FileName
Dim FileName As String = Me.UpLoadFile.PostedFile.FileName.Substring(FullFileName.LastIndexOf("\") + 1)
Dim FileType As String = Me.UpLoadFile.PostedFile.FileName.Substring(FullFileName.LastIndexOf(".") + 1)
UpLoadFile.PostedFile.SaveAs(Server.MapPath("upLoad") & "\" & FileName)
C#语法
string fullfileName=this.file1.postedFile.fileNmae;
string fileName=this.File1.PostedFile.FileName.Substring(fullFileName.LastIndexOf("\\")+1);
string type= fullFillName.Substing(fullFileName.LastIndexOf(".");
File1.PostedFile.SaveAs(server.MapPath("upLoad")+"\\"+fileName);如果有问题请与qq:407775797联系
<%@ Page language="c#" Codebehind="upload.aspx.cs" AutoEventWireup="false" Inherits="test.upload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>upload</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function addFile()
{
var str = '<input type="file" size="50" name="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<h3>多文件上传</h3>
<p id="MyFile"><input type="file" size="50" name="file"></p>
<p>
<input type="button" value="增加(Add)" onclick="addFile()"> <input type="button" value="重置(ReSet)" onclick="this.form.reset()">
<asp:Button Runat="server" Text="上传" ID="UploadButton"></asp:Button>
</p>
<p>
<asp:Label ID="strStatus" Runat="server" Font-Name="宋体" Font-Bold="true" Font-Size="9pt" Width="500px"
BorderStyle="None" BackColor="white"></asp:Label>
</p>
</div>
</form>
</body>
</HTML>.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 test
{
/// <summary>
/// upload 的摘要说明。
/// </summary>
public class upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack) this.SaveImages();
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files; /// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString()+ "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
///可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
// if (fileName != "")
// {
// fileExtension = System.IO.Path.GetExtension(fileName);
// strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString()+".rar" + "<br>");
// strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
// strMsg.Append("上传文件的文件名:" + fileName + "<br>");
// strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
// ///可根据扩展名字的不同保存到不同的文件夹
// ///注意:可能要修改你的文件夹的匿名写入权限。
// postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/ss/") + fileName);
//
// }
//
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
}
}
{
if(File_Files.PostedFile.FileName != "")
{
string strAttachName = File_Files.PostedFile.FileName.Substring (File_Piture.PostedFile.FileName.LastIndexOf("."));
string Path = Server.MapPath("../Image/MenuItem/") + System.Guid.NewGuid() + strAttachName;
File_Files.PostedFile.SaveAs(Path);
img_PackagePic.ImageUrl = Path.Replace(Server.MapPat(@"..\..\") , "http://" + Request.Url.Host + "/").Replace("\\" , "/");
}
}
看看还行啊!