Home > Back-end >  How to upload Image to Web APP (Asp.net) and hold it for processing?
How to upload Image to Web APP (Asp.net) and hold it for processing?

Time:02-04

I am trying to create a web app using asp.net (trying to get some knowledge in order to work on more advanced stuff later on). The app I'm trying to make needs to have a picture uploaded to it and then do some processing to it. Now the question, Can I have the user upload this picture display it on the page and hold it somewhere temporarily for processing without making use of a database?

Tried looking through YouTube and web but couldn't find any specific documentation.

CodePudding user response:

You can store uploaded images temporarily on the server without using a database.

One option is to save the image to the file system on the server and then read it back for processing. For example, you could use the following code to save an uploaded file to the server's file system in an "Uploads" folder:

protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
    try
    {
        string filename = Path.GetFileName(FileUpload1.FileName);
        FileUpload1.SaveAs(Server.MapPath("~/Uploads/")   filename);
        lblMessage.Text = "Upload status: File uploaded!";
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Upload status: The file could not be uploaded. The following error occured: "   ex.Message;
    }
}
}

Once the file is saved, you can read it back for processing by using the following code:

string filePath = Server.MapPath("~/Uploads/")   filename;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
 // process the image using the stream
}

Otherwise, you could also store the image in memory using a MemoryStream and then pass it to your processing method.

  • Related