Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "<input type="file" .... />" instead of asp:FileUpload

I'm modifying an existing ASP.NET project. The original author erroneously tried to create a styled asp:FileUpload by setting its visibility to hidden and just creating two custom styled browse and save buttons.

For security reason, IE does not permit this. My strategy is to instead try to use input tags with type="file", like this example. So if I set up the input like <input type="file" ID="inputFile" /> how do I access/save the file in my code behind, inputFile.SaveAs("someFile.txt");? Also (in code behind) can I do something like inputFile.HasFile or is there some other analog of this?

As per recommendations I'm trying something like the following:

             <td>
                Enabled: <asp:CheckBox ID="CheckBox2" runat="server" />    &nbsp;&nbsp;
                <div id="testFileUploader">>
                   <input type="file" id="browserHidden" runat="server" />
                   <div id="browserVisible"><input type="text" id="fileField" /></div>
                </div>
             </td>
like image 351
kmarks2 Avatar asked Apr 25 '13 14:04

kmarks2


People also ask

What is ASP FileUpload?

ASP. NET's FileUpload is an input controller used to upload files to a server. It appears on the screen with a browse button and opens up a dialogue box to choose a file or multiple files to upload from the local storage to the server. This is a server-side control provided by ASP.NET.


3 Answers

So, you can generate a random file name for the a future upload, based on the GUID at the CodeBehind of ASPX page:

HttpPostedFile filePosted = Request.Files["uploadFieldNameFromHTML"];

if (filePosted != null && filePosted.ContentLength > 0)
{
    string fileNameApplication = System.IO.Path.GetFileName(filePosted.FileName);
    string fileExtensionApplication = System.IO.Path.GetExtension(fileNameApplication);

    // generating a random guid for a new file at server for the uploaded file
    string newFile = Guid.NewGuid().ToString() + fileExtensionApplication;
    // getting a valid server path to save
    string filePath = System.IO.Path.Combine(Server.MapPath("uploads"), newFile);

    if (fileNameApplication != String.Empty)
    {
        filePosted.SaveAs(filePath);
    }
}

For Request.Files["uploadFieldNameFromHTML"] set the ID in HTML code here:

<input type='file' id='...' />

Also, don't forget to define runat="server" at the main form in ASPX page, it's better to set it at the main form and don't forget about enctype="multipart/form-data" parameter of the <form>:

<body>
    <form enctype="multipart/form-data" id="form1" runat="server">
        <input type='file' id='uploadFieldNameFromHTML' />
...
like image 63
Secret Avatar answered Oct 02 '22 14:10

Secret


Add a runat="server" to the object. This way it will work on the CodeBehid just like any asp:FileUpload control.

like image 42
melancia Avatar answered Oct 02 '22 16:10

melancia


As commented you can add the runat="server" to you input file tag.

By another hand, there is already a similar post about what you're asking for. Check this out:

Uploading Files in ASP.net without using the FileUpload server control

Hope this help

Cheers!

like image 29
MikePR Avatar answered Oct 02 '22 15:10

MikePR