0

how can i upload an image to the file systm?

hhh
  • 51
  • 4

2 Answers2

1

Use the FileUpload control.

For example (modified from the linked MSDN article), if you just want a simple form which uploads a file to a path on the server, you can start with something like this:

<%@ Page Language="C#" %>

<script runat="server">
    protected void UploadFileAction_Click(object sender, EventArgs e)
    {
        var fileStoragePath = Server.MapPath("~/Uploads");

        if (fileUploader.HasFile)
        {
            fileUploader.SaveAs(Path.Combine(fileStoragePath, fileUploader.FileName));
            outputLabel.Text = string.Format(
                "File Name: {0}<br />File Size: {1}kb<br />Content Type: {2}",
                fileUploader.PostedFile.FileName,
                fileUploader.PostedFile.ContentLength,
                fileUploader.PostedFile.ContentType
            );
        }
        else
            outputLabel.Text = "You have not specified a file.";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Upload A File</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="fileUploader" runat="server" /><br />
        <br />
        <asp:Button ID="uploadFileAction" runat="server" OnClick="UploadFileAction_Click" Text="Upload File" />&nbsp;<br />
        <br />
        <asp:Label ID="outputLabel" runat="server"></asp:Label></div>
    </form>
</body>
</html>

The FileUpload control ends up rendering to a simple <input type="file" /> (but with more attributes set, of course) on the client, as part of the overall ASP.NET form element management like any other server-side form control.

In your question, you specifically mention uploading an "image." While this code may get you there, you might also be implicitly asking a second question which is, "How can I ensure that the uploaded file is an image?" If so, you have a few options outlined very well in the answers to this question as well as this one (which refers to more answers on other questions, it's a popular topic). As always, server-side validation is necessary even though client-side validation is still recommended for a good UX. Never implicitly trust the client-side validation, always validate user input on the server as well.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Rob Nov 17 '12 at 10:58
  • @Rob: Thanks for bringing that to my attention. This was one of my older hit-and-run answers that I never went back and updated. Fixed. – David Nov 17 '12 at 12:00
  • +1 for a comprehensive answer! =) – Rob Nov 17 '12 at 12:33
1

Here is the code sample:

Your html:

<input type="file" name="Pic_0001">

NOTE: the html input control must be located within the form

Now your asp .net code:

    'this is your file name at html page
    Dim HtmlFilename As String = "Pic_0001"

    'the place to manipulate all uploaded files
    Dim collection As System.Web.HttpFileCollection
    collection = Page.Request.Files

    'for example, you have selected a picture file named hotdog.jpg in browser
    'this variable will manipulate your hotdog.jpg file
    Dim UploadedFile As System.Web.HttpPostedFile

    'retrieve the reference to your file
    UploadedFile = collection.Item(HtmlFilename)

    'this is the location to save your uploaded file
    Dim WhereToSave As String = "c:\test folder\hotdog.jpg"

    'this is your folder that will contain the uploaded file
    Dim Folderpath As String = System.IO.Path.GetDirectoryName(WhereToSave)

    'now do checking if the folder exists, if not create the folder
    'NOTE: this step is needed to prevent folder not exists error
    If System.IO.Directory.Exists(Folderpath) = False Then
        System.IO.Directory.CreateDirectory(Folderpath)
    End If

    'now actually save your file to the server
    UploadedFile.SaveAs(WhereToSave)
Predator
  • 1,267
  • 3
  • 17
  • 43