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

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

Server.MapPath - Physical path given, virtual path expected(附源码)_第1张图片

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 - Physical path given, virtual path expected(附源码)_第2张图片

使用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。


你可能感兴趣的:(Web,Service,Web,Service,C#,path,调试,异常)