How can i make a file upload, to another aspx file?

well, i'm working in domain1. I need to upload a file to domain2. in my aspx in domain1 i have (outside the main ):

<div id="divCurriculo">
    <form id="frmCurric" enctype="multipart/form-data" action="http://reports.programacontactosonae.com/uploadcv.aspx" method="post">
        <input type="hidden" name="userid" value="284" />
    <table>
        <tr>

            <td class="first">
                <label>Currículo</label>
            </td>
            <td>
                <input type="file" id="filecv" style="display:inline-block;" />
                <input type="submit" value="Enviar" style="width:70px;display:inline-block;" />
            </td>       
        <tr>            
    </table>

    </form>
</div>

So, what do i need in my receiving file in domain2 to get the file? This is what i have:

protected void Page_Load(object sender, EventArgs e)
{
      string userid = Request.Form["userid"];
      Response.Write(userid + "<br />"); // i catch, successfully, the value in the hiddenfield

      HttpPostedFile file = Request.Files[0];//here i get an error cause it can't find any file
      Response.Write(file.ToString());            
}

Upvotes: 1

Views: 174

Answers (3)

Andrew Barber
Andrew Barber

Reputation: 40160

In addition to the other answers concerning the missing enctype attribute, your code is quite brittle otherwise; you should check to make sure at least one file is present before trying to access the Request.Files collection, and display an error message if a file does not exist, letting them know to try again. Otherwise, users who forget to choose a file will get a very unhelpful error message (same one you are seeing now)

Otherwise, I'm going to assume/hope you are properly verifying/cleaning things up security-wise - like not trusting the userid value being submitted, and verifying that the content submitted isn't dangerous.

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064204

It looks like you are just missing the enctype; on the for , add an attribute:

 <form ...  enctype="multipart/form-data">...

Upvotes: 1

David
David

Reputation: 219106

Hopefully it's something this simple, but try adding enctype="multipart/form-data" to your form tag:

<form action="www.domain2.com/upload.aspx" method="post" enctype="multipart/form-data">
   <input type="hiden" id="userid" value="12345" />
   <input type="file" id="curriculo" />
   <input type="submit" id="submit"/>
</form>

Upvotes: 2

Related Questions