ASP.NET文件上载

rryofs0p  于 2023-01-27  发布在  .NET
关注(0)|答案(6)|浏览(179)

我正在尝试制作一个服务器页面(C#,asp.net2.0+)来保存从另一个页面上传的文件。
具体来说,我有一个HTML页面,其中包含

<form action="upload.aspx">

而且我不知道如何在upload.aspx中处理将文件保存在服务器上的操作。
我发现了一些例子(其中之一是:http://msdn.microsoft.com/en-us/library/aa479405.aspx),但这要求<input type=file>元素位于同一页上。
我在upload.aspx页面上抓取发布的文件时遇到了困难。
有人有什么建议吗?当文件从另一个页面发布时,我如何在aspx中获取发布的文件并将其保存到服务器?
非常感谢布雷特

envsm3lx

envsm3lx1#

1.创建Uploadfile.aspx,代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploadfile.aspx.cs" Inherits="Uploadfile" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>File Upload Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload  runat="server" ID="fuSample" />
    <asp:Button  runat="server" ID="btnUpload" Text="Upload"
            onclick="btnUpload_Click" />
            <asp:Label runat="server" ID="lblMessage" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

2.创建Uploadfile.aspx.cs,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Uploadfile : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void btnUpload_Click(object sender, EventArgs e)
  {
    //Files is folder Name, make sure it exists
    fuSample.SaveAs(Server.MapPath("/Files") + "//" + fuSample.FileName);
    lblMessage.Text = "File Successfully Uploaded";
  }
}

3.创建一个新文件:demo.html,代码如下所示,(使用iframe将Uploadfile.aspx嵌入到您的Html页面中)

<h3>Demo</h3>
<iframe height="40" width="700" src="Uploadfile.aspx">
</iframe>

4.访问/demo.html,现在您可以使用UploadFiles.aspx从html本身上传文件

rjzwgtxy

rjzwgtxy2#

使用与此类似的代码,然后将其写出到磁盘(例如,使用FileStream)

HttpFileCollection MyFileCollection;
 HttpPostedFile MyFile;
 int FileLen;
 System.IO.Stream MyStream;

 MyFileCollection = Request.Files;
 MyFile = MyFileCollection[0];

 FileLen = MyFile.ContentLength;
 byte[] input = new byte[FileLen];

 // Initialize the stream.
 MyStream = MyFile.InputStream;

 // Read the file into the byte array.
 MyStream.Read(input, 0, FileLen);

http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream%28VS.71%29.aspx

fjaof16o

fjaof16o3#

如果没有<input type=file">,您将无法完成此操作
<form action="upload.aspx">不发送文件告诉服务器将请求发送到哪里。

gblwokeq

gblwokeq4#

我做了一个小测试:
1.上传程序.aspx标记:

<form id="form1" runat="server">
<div>
    <asp:FileUpload runat="server" ID="fuTest" /><br />
    <asp:Button runat="server" ID="btnUpload" Text="Upload" PostBackUrl="~/Uploading/Upload.aspx" />
</div>
</form>
  1. Upload.aspx的代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
    FileUpload fu =  PreviousPage.FindControl("fuTest") as FileUpload;
    if (fu != null)
    {
        int length = fu.PostedFile.ContentLength;
    }
}

按钮的PostBackUrl属性将其发布到Upload.aspx页。在该页中,您可以使用Page类的PreviousPage属性查找上一页中的控件,将其强制转换为FileUpload,然后从中检索所需内容。

bbmckpt7

bbmckpt75#

namespace A8
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        class Proizvod {
            public string ceo_red, ime, proizvodjac, ram, tip, kamera,slika, ekran,sifra, cena;
            

            public Proizvod(string x) {
                ceo_red = x;
                slika = x.Split(',')[0];
                sifra = x.Split(',')[1];
                ime = x.Split(',')[2];
                proizvodjac = x.Split(',')[3];
                ram = x.Split(',')[4];
                tip = x.Split(',')[5];
                kamera = x.Split(',')[6];
                ekran = x.Split(',')[7];
                cena = x.Split(',')[8];
            }
        
        }
        List<Proizvod> proizvodi = new List<Proizvod>();
        protected void Page_Load(object sender, EventArgs e)
        {
                
            StreamReader sr = new StreamReader(@"F:\dji\A8\A8\TextFile1.txt");

            for (int i = 0; i < 5; i++)
            {
                proizvodi.Add(new Proizvod(sr.ReadLine()));
            }
            for (int i = 0; i < proizvodi.Count; i++)
            {
                bool isti = false;
                for (int j = 0; j < DropDownList1.Items.Count; j++)
                {
                    if (proizvodi[i].proizvodjac == DropDownList1.Items[j].Text) isti = true;
                }
                if (!isti) DropDownList1.Items.Add(proizvodi[i].proizvodjac);

                isti = false;
                for (int j = 0; j < DropDownList2.Items.Count; j++)
                {
                    if (proizvodi[i].ram == DropDownList2.Items[j].Text) isti = true;
                }
                if (!isti) DropDownList2.Items.Add(proizvodi[i].ram);

                isti = false;
                for (int j = 0; j < DropDownList3.Items.Count; j++)
                {
                    if (proizvodi[i].tip == DropDownList3.Items[j].Text) isti = true;
                }
                if (!isti) DropDownList3.Items.Add(proizvodi[i].tip);

                isti = false;
                for (int j = 0; j < DropDownList4.Items.Count; j++)
                {
                    if (proizvodi[i].kamera == DropDownList4.Items[j].Text) isti = true;
                }
                if (!isti) DropDownList4.Items.Add(proizvodi[i].kamera);

                isti = false;
                for (int j = 0; j < DropDownList5.Items.Count; j++)
                {
                    if (proizvodi[i].ekran == DropDownList5.Items[j].Text) isti = true;
                }
                if (!isti) DropDownList5.Items.Add(proizvodi[i].ekran);
            }
            Table1.Visible = false;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<Proizvod> trazeni = new List<Proizvod>();
            for (int i = 0; i < proizvodi.Count; i++)
            {
                if (proizvodi[i].proizvodjac == DropDownList1.Text && proizvodi[i].ram == DropDownList2.Text && proizvodi[i].tip == DropDownList3.Text && proizvodi[i].kamera == DropDownList4.Text && proizvodi[i].ekran == DropDownList5.Text)
                {
                    
                    trazeni.Add(proizvodi[i]);
                }
            }
            for (int i = 0; i < trazeni.Count; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < 9; j++)
                {
                    TableCell tc = new TableCell();
                    
                    tc.Text = trazeni[i].ceo_red.Split(',')[j];
                    tr.Cells.Add(tc);
                }
                Table1.Rows.Add(tr);
            }
            Table1.Visible = true;
        }
    }
}
j2qf4p5b

j2qf4p5b6#

在您的按钮中尝试此方法上传文件:

protected void FileUpload_Click(object sender, EventArgs e)
    {
        if ((FileUpload.PostedFile != null) && (FileUpload.PostedFile.ContentLength > 0))
        {
            string fn = Path.GetFileName(FileUpload.PostedFile.FileName);
            string SaveLocation = Server.MapPath("upload") + "\\" + fn;
            try
            {
                FileUpload.PostedFile.SaveAs(SaveLocation);
                FileUploadStatus.Text = "File Uploaded Successfully";

                try
                {
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    string[] text = File.ReadAllLines(SaveLocation);
                    File.WriteAllLines(path + "/WriteLines.txt", text);
                    Label2.Text = "File Uploaded Succesfully";
                }
                catch (Exception ex)
                {
                    FileUploadStatus.Text = "Error: " + "ex.Message";
                }
             
            }
            catch (Exception ex)
            {
                FileUploadStatus.Text = "Error: " + ex.Message;
            }
        }
        else
        {
            FileUploadStatus.Text = "Select file to upload";
        }
    }

相关问题