如何使用ASP.NET将图像存储在解决方案中?

时间:2018-06-24 05:55:28

标签: asp.net

我有一个上传图像功能,可以将图像上传到服务器,但是我只能将图像添加到服务器,直到会话结束。会话结束后,我注意到上载的图像没有存储到本地解决方案中,经过研究后发现,即使会话结束,我们也可以将上载的文件存储到我们的解决方案中,但我不确定如何实现。我对此很陌生,将不胜感激。 现在,这是我可以用来上传图片的代码:

 protected void btnConfirm_Click(object sender, EventArgs e)
    {
        string uploadedPhoto = "";
        if (uploadImage.HasFile)
        {
            string savePath;

            //Find the filename extension of the file to be uploaded.

            string fileExt = Path.GetExtension(uploadImage.FileName);

            //rename the uploaded file with name

            uploadedPhoto = txtName.Text + fileExt;
            savePath = MapPath("~/Images/profile pictures/" + uploadedPhoto);
            try
            {
                uploadImage.SaveAs(savePath);
                lblMsg.Text = "Image uploaded!";
                imgprw.ImageUrl = "~/Images/profile pictures/" + uploadedPhoto;
            }
            catch (IOException)
            {
                lblMsg.Text = "Image upload failed!";
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我看到您的代码是正确的,因为我使用相同的方式将图像上传和存储到像您一样的文件夹中。

string path = Server.MapPath("~/Photos/" + f.FileName);
f.SaveAs(path);

我可以看到您的完整代码吗?