将图像的物理路径保存在数据库中,然后将图像上传到所需的文件夹中 [英] saving a physical path of image in database and upload image to disired folder

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

问题描述

我正在尝试将图像物理路径保存在数据库中,并使用文件上载控件将图像上载到我项目中的所需文件夹..

我是上传图片的新手,所以不知道该怎么做..

我试图用这种方法做到这一点

i am trying to save image physical path in database and upload image to the disired folder in my project using file upload control..

i am new in uploading images so don''t know exactly how to do that..

i tried to do that using this method

if (FileUpload1.HasFile)
        {
            string path = Server.MapPath(".") + "\\Images\\" + FileUpload1.FileName;
            FileUpload1.SaveAs(path);

         
            string query = "insert into Products(CategoryID,ProductName,ProductImage,UnitCost,Description)select CategoryID,'" + txtproname.Text + "','" + FileUpload1.FileName + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "' from Categories where CategoryName='" + DropDownList1.Text + "'";



我不完全了解Server.MathPath.但其将图像上传到存在我的页面product.aspx(我从中上传图像)的admin文件夹中.此外,它只保存



i am not exactly understanding the Server.MathPath. but its uploading the images in the admin folder where my page products.aspx (from which i am uploading images ) is present. moreover its save just the filename like

product.gif

之类的文件名(而不是ful路径.)

我想将图像保存在根图像文件夹中.任何人都可以帮助我做到这一点.我想将完整的物理路径保存为数据库中的

(not the ful path.)

i want to save image in the root images folder. can anyone pls help me to do that. and i want to save complete physical path in database as

~/images/product.gif

推荐答案

如果传递给MapPath的路径不是以正斜杠或反斜杠开头返回相对于页面所在目录的路径.这是在admin文件夹中上传图像时看到的内容.以下应该产生您想要的.
If the path you pass to MapPath does not start with either a forward or back slash the method returns a path relative to the directory the page is in. Which is what you are seeing when images are uploaded in the admin folder. The following should produce what you want.
string path = Server.MapPath(@"/Images") + FileUpload1.FileName;



至于数据库问题,由于您只是传入仅包含文件名的FileUpload1.FileName,因此仅插入product.gif.如果要使用完整路径,则需要传递path变量.您应该真正摆脱使用字符串连接编写SQL语句的坏习惯.使用字符串连接并没有缺点,而且有很多缺点.



As for the DB issue it is only inserting product.gif since you are just passing in FileUpload1.FileName which contains just the filename. If you want the full path you need to pass in the path variable. You should really get out of the bad habit of writing you SQL statements using string concatenation. There are no pros to using string concatenation, and plenty of cons.


如果要保存根路径,例如Path ="Images \ product.gif"
If you want to save rooted path like Path ="Images\product.gif"
if (FileUpload1.HasFile)
{
    string path =  @"Images\" + FileUpload1.FileName;
    FileUpload1.SaveAs(Server.MapPath("~") + @"\" + path);
    // Save process strfilename = path 
}


hi,

try to give following format.,


<code>if (FileUpload1.postedfile.contentlength>0)
{
  string path = Server.Mappath(@"~/images/"+'"+FileUpload1.Filename+"')
  FileUpload1.SaveAs(path);

//now save the path value to your database.

   string query = "insert into Products(CategoryID,ProductName,ProductImage,UnitCost,Description)select CategoryID,'" + txtproname.Text + "','" + path  + "'," + txtproprice.Text + ",'" + txtprodesc.Text + "' from Categories where CategoryName='" + DropDownList1.Text + "'";
}
else
{
messagebox("file content length is zero");
}</code>
i hope this is useful to you...
take care


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

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