我希望将文件从一个页面上传到另一个页面,然后保存到我的在线服务器。
我的代码是
第一页:
<html>
<body>
<form action="http://localhost:3132/Sample/api/Test.aspx?frm=31" method="post" target="_blank">
Name: <input type="text" name="f1" id="f1"><br>
Email: <input type="text" name="email" id="email"><br>
FileName: <input type="file" name="file" id="file"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
第二页:Entries.aspx
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Data.DataTable dtData=new System.Data.DataTable();
foreach (string strKey in Request.Form.AllKeys)
{
if(strKey.Equals("data.xml") || dtData.Columns.Contains(strKey))
continue;
dtData.Columns.Add(strKey);
System.Data.DataRow dr=dtData.NewRow();
if(dtData.Rows.Count>0)
dr=dtData.Rows[0];
dr[strKey]=Request.Form[strKey];
if(dtData.Rows.Count<1)
dtData.Rows.Add(dr);
}
string strFileName = string.Empty;
string newFilename = string.Empty;
string fileName = System.IO.Path.Combine(Server.MapPath("~/APIUploaded"), Request.Params["filename"].ToString());
newFilename = Guid.NewGuid().ToString();
System.IO.FileInfo fInfo = new System.IO.FileInfo(fileName);
newFilename = string.Format("{0}{1}", newFilename, fInfo.Extension);
strFileName = System.IO.Path.Combine(Server.MapPath("~/APIUploaded"), newFilename);
using (System.IO.FileStream fileStream = System.IO.File.Create(strFileName))
{
/*Getting stream from the Request object.*/
using (System.IO.Stream stream = Request.InputStream)
{
int byteStreamLength = (int)stream.Length;
byte[] byteStream = new byte[byteStreamLength];
/*Reading the stream to a byte array.*/
stream.Read(byteStream, 0, byteStreamLength);
/*Writing the byte array to the harddisk.*/
fileStream.Write(byteStream, 0, byteStreamLength);
}
}
通过第二页,我获得了带有上传文件名的表单值。当我要保存到我的在线服务器文件夹时,文件内容为空。是否可以通过在asp.net中使用文件上传控件将上传文件从一个页面上传到另一个页面?
答案 0 :(得分:1)
您需要将enctype添加到表单中:
<form action="http://localhost:3132/GoLeadsUpdate/api/Entries.aspx?frm=31"
method="post" target="_blank" enctype="multipart/form-data">
.
.
.
</form>
我认为你必须看Request.Files
:http://msdn.microsoft.com/en-us/library/system.web.httprequest.files(v=vs.110).aspx