在线站点中的图像上传问题 [英] IMAGE Uploading problem in An Online Site

查看:110
本文介绍了在线站点中的图像上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在我的内联网站中上传和显示图像.
如果我将站点部署在IIS服务器中,则代码可以正常运行.
但是图像无法在在线服务器上加载.

代码如下.


I''m not able to upload And Display Image in My Inline Site.
If i deploy the Site in IIS server the code is running without any problem.
But the images are not able to load on online server.

the code is as under.


try
       {
           Boolean fileOK = false;
           String path = Server.MapPath("~/Assets/");
           String path1 = Server.MapPath("~/Assets/");
           if (txtBIP.HasFile&&txtTIP.HasFile)
           {
               String fileExtension =
                   System.IO.Path.GetExtension(txtBIP.FileName).ToLower();
               String fileExtension1 =
                   System.IO.Path.GetExtension(txtBIP.FileName).ToLower();
               String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
               for (int i = 0; i < allowedExtensions.Length; i++)
               {
                   if (fileExtension == allowedExtensions[i] && fileExtension1 == allowedExtensions[i])
                   {
                       fileOK = true;
                   }
               }
           }
           if (fileOK)
           {
               try
               {
                   txtBIP.PostedFile.SaveAs(path
                       + txtBIP.FileName);
                   txtTIP.PostedFile.SaveAs(path
                       + txtTIP.FileName);
                   Label4.Text = "File uploaded!";
               }
               catch
               {
                   Label4.Text = "File could not be uploaded.";
               }
           }
           else
           {
               Label4.Text = "Cannot accept files of this type.";
           }
           if (txtBIP.HasFile&&txtTIP.HasFile)
           {
               SqlCommand add = new SqlCommand("insert into gallery values('" + ImageId + "','" + "~/Assets/" + txtBIP.FileName.ToString() + "','" + "~/Assets/" + txtTIP.FileName.ToString() + "')", con);
               con.Open();
               add.ExecuteNonQuery();
               con.Close();
               Response.Redirect("~/AddImages.aspx");
           }
           else
           {
               Label4.Text = "Please select a file first";
           }
       }
       catch (Exception er)
       {
           Label4.Visible = true;
           Label4.Text = "Sorry Error in Processing the Request Please Contact System Administrator";
       }
       finally
       {
           con.Close();
       }



请为我找到合适的解决方案

感谢



please find a proper solution for me

thanks

推荐答案

<pre lang="xml"><table cellpadding="0" cellspacing="0" border="0">
                                    <asp:UpdatePanel runat="server" ID="imgUpDate">
                                        <ContentTemplate>
                                            <tr>
                                                <td>
                                                    <asp:Image runat="server" ID="imgPhoto" Width="130px" Height="110px" ImageUrl="~/Images/empimg.JPG"
                                                        BorderStyle="Double" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:FileUpload runat="server" ID="upbtn" />
                                                    <asp:RegularExpressionValidator runat="server" ID="rgeImage" ControlToValidate="upbtn"
                                                        ErrorMessage="Only (.jpg,.jpeg) Allowed." ValidationExpression="^.*\.(jpg|jpeg|JPG|JPEG)


"></asp:RegularExpressionValidator> </td> </tr> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="btnAdd" /> </Triggers> </asp:UpdatePanel>
"></asp:RegularExpressionValidator> </td> </tr> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="btnAdd" /> </Triggers> </asp:UpdatePanel>




在apsx页面上进行书写.

在CS页面上.




wirite this on apsx page.

on cs page.

<pre lang="cs">protected void Insert_Image(int empid)
  {
      try
      {
          if (upbtn.HasFile)
          {
              string img_path = Server.MapPath("~/EmpPhoto\\");
              string extension = Path.GetExtension(upbtn.FileName);
              string imgstr = img_path + empid + extension;

              FileInfo f = new FileInfo(imgstr);
              if (f.Exists)
              {
                  System.IO.File.Delete(imgstr);
                  upbtn.SaveAs(img_path + empid + extension);
              }
              else { upbtn.SaveAs(img_path + empid + extension); }
          }
      }
      catch (Exception ex)
      {

          throw ex;
      }
  }





现在,在表中成功插入数据后,请调用此函数.





Now call this function after the success of insert data in table.


我想到的两件事可能在您的图像上载代码上下文中没有涉及.

1)您要将这些文件上传到的目录必须具有写权限,这些写权限已分配给IIS管理控制台中的虚拟目录.

2)OS文件夹需要正确的权限,以允许用户标识也可以写入此文件夹.如果要进行任何形式的模拟,则需要某种方式将身份添加到文件夹访问控制列表"中.如果不是这种情况,那么您将需要分配Internet Guest Account权限.

3)如果文件显示有问题,则MIME Map中可能有问题,您可以使用IIS管理员控制台或C:\ WINDOWS \ system32 \ inetsrv \ MetaBase.xml文件进行编辑.
在检查完所有内容之后,强烈建议您查看是否有例程从上传中排除文件扩展名的特定列表,然后将其重构为另一个返回true或false条件的命令.这与验证不上传有关,可以帮助分离这两项的代码内聚性.

我也注意到了

There are two things that I can think of that may not be covered in the context of your code here for image upload.

1) the directory that you are uploading these files to needs to have write privileges which are assigned to the virtual directory in the IIS admin console.

2) The OS folder needs the correct permissions to allow the users identity to write to this folder as well. If you are doing any kind of impersonation you will need some way of adding the identity to the folder Access Control List. If this is not the case then you will need to assign Internet Guest Account permissions.

3) If the file is having issues being displayed there could be issues in your MIME Map you can edit this either with the IIS admin consol or the C:\WINDOWS\system32\inetsrv\MetaBase.xml file.

After all that has been checked Looking through your code, I would strongly recommend if you have a routine to exclude a particular list of file extensions from uploading, you should refactor that into another command that returns a true or false conditions. That is more related to validation not uploading it can help decouple the code cohesion of these two items.

I also noticed

txtBIP.PostedFile.SaveAs(path +txtBIP.FileName);                   txtTIP.PostedFile.SaveAs(path + txtTIP.FileName);<br />



我不确定这有什么意义,但是BIP是否选中TIP不是.

快速说明:您应该使用SqlParamaters将参数传递给查询,而不是直接内联代码.

最后,我注意到您正在使用〜/Assets/保存虚拟路径,这是.NET只能识别的虚拟路径.如果将其提取到标准链接标记中,则不使用对VirtualPathUtility的调用就不会将其重新解释为可用路径.



I am not sure what the relevance of this is but BIP is checked TIP is not.

Quick note you should pass parameters into queries using SqlParamaters not inlining code directly.

Finally, I noticed that you are saving the virtual path using ~/Assets/ which is a virtual path that only .NET recognizes. if you extract this into a standard link tag it won''t reinterpret into a usable path without using calls to VirtualPathUtility.


这篇关于在线站点中的图像上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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