将图像上传到服务器 [英] Upload image to server
问题描述
我正在尝试将图片上传到服务器但是错误不是有效的虚拟路径
i希望通过fileuploader控件将图像上传到媒体文件夹。 />
请指导。
Quote:string a = HttpContext.Current.Server.MapPath( http://printofast.com/media/);
string fileName = System.IO.Path.Combine(Server.MapPath(a,fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);
我尝试过:
string a = HttpContext.Current.Server.MapPath(http://printofast.com/media/);
string fileName = System.IO.Path.Combine (Server.MapPath(a,fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);
---
// fileuploadPimage.SaveAs(Config.GetConfigValueAsString(ProductImagesPath) )+ strfilename);
//fileuploadPimage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings[\"ImagePath],strfilename));
服务器。 Mappath将像〜\ resources \ images'\\ myPicture.jpg这样的虚拟路径转换为运行IIS的服务器上的文件系统理解的正确限定路径 - 其中〜是您网站的根文件夹。您无法将其提供给URL,并希望将其转换为文件系统地址。
如果您要将文件保存到您的网站,请尝试
fileupload.SaveAs(Path.Combine(Server.MapPath( 〜/ Media),fileuploadPimage.FileName);
但请注意,不同的用户对不同的文件使用相同的名称是很常见的。我会使用数据库存储文件名,上传文件的用户和Guid ID列,并使用Guid值作为实际文件名将文件保存到磁盘。这样,不同的用户可以安全地拥有相同名称的文件,他们不会相互干扰。
尝试将图像插入文件夹并将文件路径保存到数据库。
< pre lang =C#> string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.Sav eAs(Server.MapPath( 〜/ Imgdata / + filename));
SqlCommand cmd = new SqlCommand( Insert到Image_data(ImgTitle,Imgdata)值(@ ImgTitle,@ Imgdata),con);
con.Open();
cmd.Parameters.AddWithValue( @ ImgTitle,TextBox1.Text);
cmd.Parameters.AddWithValue( @ Imgdata, / imgdata / + filename);
cmd.ExecuteNonQuery();
lblerror.ForeColor = Color.Green;
lblerror.Text = 插入成功;
con.Close();
Hi ,
I am trying to upload image to server but getting error not a valid virtual path
i want to upload image to media folder by fileuploader control .
please guide.
Quote:string a = HttpContext.Current.Server.MapPath("http://printofast.com/media/");
string fileName = System.IO.Path.Combine(Server.MapPath(a, fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);
What I have tried:
string a = HttpContext.Current.Server.MapPath("http://printofast.com/media/");
string fileName = System.IO.Path.Combine(Server.MapPath(a, fileuploadPimage.FileName);
fileuploadPimage.SaveAs(fileName);
---
// fileuploadPimage.SaveAs(Config.GetConfigValueAsString("ProductImagesPath") + strfilename);
//fileuploadPimage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["ImagePath"] ,strfilename));
Server.Mappath converts a virtual path like "~\resources\images\myPicture.jpg" into a "proper" qualified path that the file system on the server running IIS understands - where "~" is the root folder for your website. You can't feed it a URL and expect it to translate that into a file system address.
If you are trying to save a file into your website, then try
fileupload.SaveAs(Path.Combine(Server.MapPath("~/Media"), fileuploadPimage.FileName);
But be aware that it's very common for different users to have the same name for different files. I'd use a DB to store the filename, the user that uploaded it, and a Guid ID column, and save the file to the disk using the Guid value as the actual filename. That way, different users can safely have files with the same name and they don't interfere with each other.
Try this to insert the image to folder and save the file path to database.
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("~/Imgdata/" + filename)); SqlCommand cmd = new SqlCommand("Insert into Image_data(ImgTitle,Imgdata)values(@ImgTitle,@Imgdata)",con); con.Open(); cmd.Parameters.AddWithValue("@ImgTitle", TextBox1.Text); cmd.Parameters.AddWithValue("@Imgdata", "/imgdata/" + filename); cmd.ExecuteNonQuery(); lblerror.ForeColor = Color.Green; lblerror.Text = "Insert Success"; con.Close();
这篇关于将图像上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!