使用c# asp.net时没有得到正确的文件路径
本文关键字:路径 文件 asp net 使用 | 更新日期: 2023-09-27 18:02:40
我正在尝试显示一些图像,使用c# asp.net从数据库抓取,但我得到一些错误。
错误:'/'应用程序出现服务器错误。
C: ' ' Odiya_Doctor_Client ' Odiya_Doctor_Client ' ASP项目ODIYA_Doctor_Admin '上传'横幅' 2015 - 07 - 09 - _01 50 - 41 - pm_medical横幅——icons.jpg
描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。
异常详细信息:System.IO.FileNotFoundException: C:'ASP project'Odiya_Doctor_Client'Odiya_Doctor_Client'ODIYA_Doctor_Admin'Upload'Banner'2015-07-09_01-50-41- pm_medicalbanner -with-icon .jpg
我在下面解释我的代码。
index.aspx:
<img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64("/ODIYA_Doctor_Admin/Upload/Banner/" + Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />
index.aspx.cs:
protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(Server.MapPath(imageDirectory));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}
实际上我想访问一个图像,这是存在于c:'ASP'ODIYA_Doctor_Admin'Upload'Banner
文件夹,但这里是我的索引。aspx页在c:'ASP'Odiya_Doctor_Client'Odiya_Doctor_Client
文件夹内。在错误信息中,路径是C:'ASP project'Odiya_Doctor_Client'Odiya_Doctor_Client'ODIYA_Doctor_Admin'Upload'Banner'2015-07-09_01-50-41-PM_Medical-banner-with-icons.jpg
,我想从这个路径中删除'Odiya_Doctor_Client'Odiya_Doctor_Client
。所以请帮我解决这个错误
使用fileupload控件代替server.mappath作为文件位置因为服务器。Mappath总是查看您的解决方案文件夹
private void getPicture()
{
string location = FileUpload1.PostedFile.FileName;
resizeAndConvertToBase64(location, 100,100);
}
protected string resizeAndConvertToBase64(string imageDirectory, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
System.Drawing.Image srcImage = imageDirectory;
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}
I would suggest you to do this way:
1. Create a application setting:
<appSettings >
<add key="ImgFolderPath" value="c:'ASP'ODIYA_Doctor_Admin'Upload'Banner"/>
</appSettings>
2. Change your aspx code:
<img runat="server" id="imgCtrl" src='<%# resizeAndConvertToBase64( Convert.ToString(Eval("Bnr_Image")),1920,680) %>' class="ls-bg" />
3. Change your .cs page code:
protected string resizeAndConvertToBase64(string imageName, int newWidth, int newHeight)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
string path = ConfigurationManager.AppSettings["ImgFolderPath"].ToString();
System.Drawing.Image srcImage = System.Drawing.Image.FromFile(path+"/"+imageName));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
MemoryStream ms = new MemoryStream();
newImage.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
return "data:image/gif;base64," + base64Data;
}