asp.net将图片上传到服务器

时间:2013-03-21 10:52:48

标签: asp.net

我正在将图像上传到asp.net页面中的服务器。这是代码..

代码

protected void Button2_Click1(object sender, EventArgs e)
        {

           //resimyolu = "~/r/" + FileUpload1.FileName;
           FileUpload1.SaveAs(Server.MapPath("~/r/a.png"));
           FileUpload1.SaveAs("d:/upresim/a.png");
           Image1.ImageUrl = "~/r/a.png";

        }

'r'文件夹是我项目中的图像文件夹。当我在运行时将图片添加到'r'文件夹时,它在本地IIS上成功运行,但是当我发布我的项目并将其托管在服务器上时(远程服务器)网页在浏览器中打开,但是当我单击button2将客户端图像(从他的计算机中选择的客户端用户)添加到项目时,会出现如下错误。

Runtime Error 
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. 

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

2 个答案:

答案 0 :(得分:0)

根据错误消息中的建议,禁用web.config上的自定义错误以查看问题所在。

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

答案 1 :(得分:0)

问题在于这一行。

FileUpload1.SaveAs("d:/upresim/a.png");

将其更改为有用。

FileUpload1.SaveAs("d:\\upresim\\a.png");

如果您的代码,请避免使用此类硬编码路径。更换机器时会遇到问题。 Server.MapPath永远是一个更好的方式。如果您不想保存在Web目录中,则应始终从配置中获取基本路径。