views:

58

answers:

4

Hi there,

I am trying to make a server page (C#, asp.net 2.0+) to save an uploaded file from another page.

Specifically, I have an HTML page with a

<form action="upload.aspx"> 

and I can't figure out how to handle saving the file on the server in upload.aspx.

I found a few examples (one being: http://msdn.microsoft.com/en-us/library/aa479405.aspx) but that requires the <input type=file> element to be on the same page.

I am having difficulties with grabbing the posted file on my upload.aspx page.

Anyone have any pointers? How can I grab a posted file in aspx and save it to the server when the file is posted from another page?

Many thanks, Brett

A: 

Use code similar to this and then write it out to disk (using, say, 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

Raj Kaimal
+1  A: 

You can't do it without the <input type=file">

<form action="upload.aspx"> doesn't send a file tells the server where to send the request to.

Conrad Frix
A: 

I made a small test case:

  1. Uploader.aspx markup:

    <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>
    
  2. Codebehind from 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;
        }
    }
    

The PostBackUrl property of the button posts it to the Upload.aspx page. There you can use the PreviousPage property of the Page class to find the control from the previous page, cast it to FileUpload, and retrieve what you want from it.

XIII
A: 

1.Create Uploadfile.aspx 2.Embed the Uploadfile.aspx in Your Html page using iframe

    <%@ 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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>


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
        fuSample.SaveAs(Server.MapPath("Files") + "//" + fuSample.FileName);
        lblMessage.Text = "File Successfully Uploaded";
    }
}

then embed your aspx page in Html as follow,

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

now you can be able to upload your file from html itself,by using UploadFiles.aspx.

Ramakrishnan