将图像保存在虚拟文件夹及其数据库路径中 [英] save images in virtual folder and its path to database

查看:62
本文介绍了将图像保存在虚拟文件夹及其数据库路径中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子tbl_Image1
字段是:sr(int),用户名(varchar(50)),文件名(varchar(50)),文件路径(varchar(200))

我的图片上传编码如下所示

i have one table tbl_Image1
fields are: sr (int), UserName (varchar(50)), Filename (varchar(50)), FilePath (varchar(200))

my coding for image upload is as below

if (FileUpload1.PostedFile != null)
       {
           if (FileUpload1.PostedFile.ContentLength > (1024*1024))
           {
               Label37.Text = "Upload status: The file has to be less than 1 MB. Please resize your photo and than upload it again.";
           }
           else
           {
           System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
           int imageHeight = imageToBeResized.Height;
           int imageWidth = imageToBeResized.Width;
           int maxHeight = 660;
           int maxWidth = 560;
           imageHeight = (imageHeight * maxWidth) / imageWidth;
           imageWidth = maxWidth;
           if (imageHeight > maxHeight)
           {
               imageWidth = (imageWidth * maxHeight) / imageHeight;
               imageHeight = maxHeight;
           }
           Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
           System.IO.MemoryStream stream = new MemoryStream();
          // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
           stream.Position = 0;
           byte[] image = new byte[stream.Length + 1];
           stream.Read(image, 0, image.Length);

           string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
           //Save files to disk
           string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
           //Path.Combine(Server.MapPath("~/images/store"), imageName);
           //string imagename = TextBox20.Text + "1" + extension;
           bitmap.Save(Server.MapPath("profimgs/" + TextBox20.Text + "1" + extension));
           //Add Entry to DataBase


           String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
           SqlConnection con = new SqlConnection(strConnString);
           string strQuery = "insert into tbl_Image1 (UserName, FileName, FilePath) values ('" + TextBox20.Text + "',@FileName, @FilePath)";
           SqlCommand cmd = new SqlCommand(strQuery);
           cmd.CommandTimeout = 0;
           cmd.Parameters.AddWithValue("@FileName", FileName);
           cmd.Parameters.AddWithValue("@FilePath", "profimgs/" + TextBox20.Text + "1" + extension);
           cmd.CommandType = CommandType.Text;
           cmd.Connection = con;

               try
               {
                   con.Open();
                   cmd.ExecuteNonQuery();
               }

               catch (Exception ex)
               {
                   Response.Write(ex.Message);
               }

               finally
               {
                   con.Close();
                   con.Dispose();
                   Response.Redirect("~/free User/addimage.aspx");
               }
           }

       }



我的问题是我不知道该格式是否可以定义图像的虚拟路径.
我的文件夹是profimgs,它仅位于服务器的根目录.
尝试过类似的事情,



my problem is just that i dont know the format fto define the virtual path for the image.
my folder is profimgs and its on the root of the server only.
have tried different things like,

bitmap.Save(Server.MapPath("profimgs/" + imagename));
bitmap.Save(Server.MapPath("~") + @"\profimgs\" + imagename);
bitmap.Save(Server.MapPath("http://www.mywebsitename.com/profimgs/" + imagename));



但他们都没有工作. :(
我只需要知道如何指定要在图像中存储在数据库中的虚拟路径?"



but none of them worked. :(
i only need to know "HOW TO SPECIFY THE VIRTUAL PATH TO STORE IN THE DATABASE FOR THE IMAGE?"

推荐答案

通过使用Server.MapPath(),就不会使用虚拟路径.
因此,〜/Yourpath/"应为在数据库表中用作值的格式.
By using Server.MapPath() you are not using the virtual path.
So "~/Yourpath/" should be format to use as value in your databasetable.


Server.MapPath(~/profimgs/")

会将图像保存/检索到网站根目录下的"profimgs"文件夹中.
Server.MapPath(~/profimgs/")

will save/retrieve the images in a folder "profimgs" on root of the website.


它在您的网站中创建一个文件夹并将所有图像放在这些文件夹中

如果(hpf1.ContentLength> 0)
{

如果(!Directory.Exists(Server.MapPath()+"/yourfoldername/))
{
Directory.CreateDirectory(Server.MapPath(")+"/yourfoldername/");
}
hpf1.SaveAs(Server.MapPath(")+"/您的文件夹名称/" + System.IO.Path.GetFileName(hpf1.FileName));
}
并在数据库中发送文件名
使用
字符串filename = hpf1.FileName;
its create a folder in your web site and put all images on these folder

if (hpf1.ContentLength > 0)
{

if (!Directory.Exists(Server.MapPath() + "/yourfoldername/))
{
Directory.CreateDirectory(Server.MapPath("") + "/yourfoldername/");
}
hpf1.SaveAs(Server.MapPath("") + "/yourfoldername/" + System.IO.Path.GetFileName(hpf1.FileName));
}
and sending the file name in database
use
string filename=hpf1.FileName;


这篇关于将图像保存在虚拟文件夹及其数据库路径中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆