与文件上传负载映像文件服务器,并尝试阅读 [英] load image file with fileUpload to server and try to read it

查看:226
本文介绍了与文件上传负载映像文件服务器,并尝试阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题的主题。我的程序会要求用户提供文件上传加载图片,然后此文件具有同时显示的ListBox中的文件名,并在图像的图片。一切都显示,除了在图像控制画面完成。我已经读了很多论坛,并试图用〜/ \\改变URL,但没有什么帮助。 B.t.w.,我试图改变在文件夹属性访问安全性,但它不会仍然工作。这就是为什么,我请你帮助我。这是加载文件和必须表明它在我的code的一部分:

I have some problem with subject. My program asks user to load picture with fileUpload and then this file has to show both in listBox as filename and in Image as picture. Everything is done except showing picture in Image control. I've already read a lot of forums and tried to change url with ~ / \ but nothing help. B.t.w., I tried to change access security in properties for folder, but it doesn't still work. That is why, I ask you to help me. This is part of my code which load file and must show it:

        string folderPath = Server.MapPath(@"~\images\");
        FileUpload1.SaveAs(folderPath + filename);
        ListBox1.Items.Add(filename);
        folderPath = System.Text.RegularExpressions.Regex.Replace(folderPath, @"\\", @"/"); //tried to do with and without this line
        Image1.ImageUrl = folderPath + filename;

哪里是我的错吗?如果我在地址线本地主机\\图像输入\\ pic.jpg它的显示以及

Where is my fault? If I enter in address-line localhost\images\pic.jpg it's shown well

推荐答案

你有你的文件路径混淆。 使用Server.Mappath采用相对路径,并得到对于相对路径的物理文件路径。影像不能使用此 - 它应该使用相对路径

You have your file paths confused. Server.MapPath takes a relative path and gets a physical file path for that relative path. Images can't use this -- it should use the relative path.

您code应该是这样的:

Your code should look like this:

string relPath = String.Format("~/images/{0}", filename);
string filePath = Server.MapPath(relPath);
FileUpload1.SaveAs(filePath);
ListBox1.Items.Add(filename);
Image1.ImageUrl = relPath;

这篇关于与文件上传负载映像文件服务器,并尝试阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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