Server.MapPath - Physical path given, virtual path expected(附源码)

来源:互联网 发布:家电销量数据 编辑:程序博客网 时间:2024/04/30 07:21

.Net Web Developer的Web Service,接收C#客户端发送的文件,服务器使用了物理绝对路径(Physical path),跟踪调试,Web Service的asmx执行存放文件操作时会弹出"Physical path given, virtual path expected"(需要相对路径)异常。

Web Service握手和收发文件测试图

借助Google搜索引擎,找到同类问题

Server.MapPath - Physical path given, virtual path expected

http://stackoverflow.com/questions/5039725/server-mappath-physical-path-given-virtual-path-expected

借助Server.MapPath进行相对路径文件的存区:

Server.MapPath方法的应用方法

http://www.cnblogs.com/Showshare/archive/2007/04/23/723965.html

使用Server.MapPath调试成功图

附资源链接和代码段

C#怎么通过WebService 上传图片

http://bbs.csdn.net/topics/390550918

使用了三楼大神的一段代码,如下

        [WebMethod]        public string upfilebyte(byte[] b, ref string FileName)        {            try            {                MemoryStream m = new MemoryStream(b);                using (FileStream fs = File.Open(Server.MapPath(@"\upfile\" + FileName), FileMode.Create))                {                    m.WriteTo(fs);                    m.Close();                    fs.Close();                    delLastTimeFile();                    return "上传成功!";                }            }            catch (Exception xx) { return xx.Message; }        }        [WebMethod]        public byte[] downfilebyte(ref string FileName)//下载        {            try            {                using (FileStream fs = File.Open(Server.MapPath(@"\upfile\" + FileName), FileMode.Open))                {                    byte[] b = new byte[fs.Length];                    fs.Read(b, 0, Convert.ToInt32(fs.Length));                    fs.Close();                    FileName = "";                    return b;                }            }            catch (Exception xx) { FileName = xx.Message; return null; }        }
可以发现,c02645大神有使用Server.MapPath,我修改代码过程中走了弯路,学会使用Server.MapPath。


0 0
原创粉丝点击