简体   繁体   English

将图像保存到IIS localhost中的文件夹

[英]Saving image to a folder in IIS localhost

I have to save images to a folder located in "c:\\inetpub\\wwwroot\\" and named as "UploadedImages". 我必须将图像保存到位于“ c:\\ inetpub \\ wwwroot \\”的文件夹中,并命名为“ UploadedImages”。 Here is my code: 这是我的代码:

         public string SaveImage(string base64,int compno)
{
    string res = "";
    try
    {

        using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64)))
        {
            using (Bitmap bm2 = new Bitmap(ms))
            {
                bm2.Save(Server.MapPath("~/UploadedImages/ID"+compno+".jpg"));
            }
        }
        res = "done";
    }
    catch (Exception ex) {
        res = ex.ToString();
    }
    return res;
}

but it throws "A generic error occured in GDI+ at System.Drawing.Image.Save" exception. 但它会引发“ System.Drawing.Image.Save处的GDI +中发生一般错误”异常。 What am I doing wrong? 我究竟做错了什么? This code works fine when saving image locally as 当将图像本地保存为

       bm2.Save("D:Embasy\UploadedImages\ID"+compno+".jpg"));

What changes do I need to make to save images in localhost directory? 我需要进行哪些更改才能将图像保存在localhost目录中?

Your not going to believe this -- the site running v1.1 had a virtual directory set-up which was mapped to the directory in which the image was saved to -- things worked fine. 您不会相信这一点-运行v1.1的站点有一个虚拟目录设置,该目录已映射到将映像保存到的目录-一切正常。

The v2.0 site also had the same virtual directory name, but the physical path was different -- I changed the path to point to the same directory as the v1.0 site and now the code works. v2.0站点也具有相同的虚拟目录名称,但是物理路径不同-我将路径更改为指向与v1.0站点相同的目录,现在代码可以正常工作了。

So in short -- you were right about the "path must exist". 简而言之,您对“路径必须存在”是正确的。

Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Windows或ASP.NET服务中不支持使用System.Drawing命名空间中的类。 Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions. 尝试从这些应用程序类型之一中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。 For a supported alternative, see Windows Imaging Components. 有关支持的替代方法,请参见Windows Imaging Components。

and kindly refer this link 并请参考此链接

http://msdn.microsoft.com/en-us/library/xs6ftd89.aspx http://msdn.microsoft.com/en-us/library/xs6ftd89.aspx

When you are using Server.Mappath 使用Server.Mappath时

bm2.Save(Server.MapPath("~/UploadedImages/ID"+compno+".jpg"));

"~(tield)" : is point to project/application root folder c:\\inetpub\\wwwroot\\yourproject “〜(tield)” :指向项目/应用程序的根文件夹c:\\ inetpub \\ wwwroot \\ yourproject

then find remaining your path /UploadedImages and then create ID1.jpg. 然后找到剩余的路径/ UploadedImages,然后创建ID1.jpg。

But your imagefolder "UploadImages" is exist in d: not in c:\\inetpub\\wwwroot\\yourproject 但是您的图像文件夹“ UploadImages”存在于d:中, 而不存在于c:\\ inetpub \\ wwwroot \\ yourproject中

if you want to exist your image folder in D: then you should to create virtual directory and then you need to apply path as relevant 如果要在D中存在图像文件夹,则应创建虚拟目录 ,然后将路径应用为相关路径

My problem actually was that every time i published website, I replaced the "UploadedImages" folder too and thus permissions were changed. 我的问题实际上是,每次我发布网站时,我也都替换了“ UploadedImages”文件夹,因此更改了权限。 So, i didnt replaced the folder again after changing its permissions and creating "everyone" group and giving full rights to it. 因此,在更改其权限并创建“所有人”组并赋予其完全权限后,我没有再次替换该文件夹。 Now code is working perfectly :) 现在代码可以完美运行了:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM