在asp.net中保存上传的图像c#

问题描述:





我使用fileupload控件将图像上传到asp.net的网页。现在我正在刷新页面或退出页面图像消失了。任何人都可以帮我解决这个问题。



提前致谢...

http://www.codeproject.com/ script / Answers / Post.aspx?aid = 464791#

aspx.cs



Hi,

I uploaded a image into a webpage in asp.net using fileupload control.Now when I am refreshing the page or logging out the page the image disappears.Can anyone help me in this issue.

Thanks in advance...
http://www.codeproject.com/script/Answers/Post.aspx?aid=464791#
aspx.cs

protected void Button2_Click(object sender, EventArgs e)
{
    string path = Server.MapPath("croppedImages/");
    if(FileUpload1.HasFile)
    {
        string ext=Path.GetExtension(FileUpload1.FileName);
        if (ext== ".jpg" || ext== ".png")
        {
            FileUpload1.SaveAs(path+FileUpload1.FileName);
            string name = "~/croppedImages/" + FileUpload1.FileName;
            string s = "insert into immage values('"+TextBox1.Text.Trim()+"','"+name+"')";

            SqlCommand cmd= new SqlCommand(s,con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("Your file has been uploaded");
        }
        else
        {
            Response.Write("You can upload only jpg and png files");
        }
    }
    else
    {
        Response.Write("Please select a file");
    }
    Session["ImageBytes"] = FileUpload1.FileBytes;
    ImagePreview.ImageUrl = "~/Handler.ashx";

}

FileUpload控件除了上传文件外什么都不做。当它执行它的回发时你需要将数据保存到硬盘以便永久存储。

在页面刷新时你需要再次从磁盘加载图像,你可以使用asp :用于显示它的图像标记。



请参阅本页底部的示例,以使用FileUpload控件将文件保存到磁盘: http://msdn.microsoft.com/en-us/library /system.web.ui.webcontrols.fileupload(v=vs.100).aspx [ ^ ]



希望这个在你的路上帮助你。
The FileUpload control does nothing other than upload the file. When it is performing it''s postback you need to save the data to the harddisk for permanent storage.
On page refresh you will need to load the image from the disk again, you can use the asp:Image tag to display it.

See the sample at the bottom of this page for saving a file to the disk using the FileUpload control: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload(v=vs.100).aspx[^]

Hope this helps you on your way.