【问题标题】:File upload through web service: given file path format not supported通过 Web 服务上传文件:不支持给定的文件路径格式
【发布时间】:2013-02-15 00:12:51
【问题描述】:

我正在尝试使用网络服务将文件上传到网络服务器。问题是每次我尝试使用引用的 Web 方法时都会收到“不支持给定文件格式”的异常。

这是我的应用程序中的代码:

Service1 upload = new Service1();
FileStream fs = new FileStream("ciao.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(fs);
int length = new FileInfo("ciao.txt").Length;
byte[] buffer = br.ReadBytes((Int32)length);            
upload.WriteFile(buffer, "ciao.txt"); // <-- Exception
br.Close();
fs.Close();

这是http://MySite.somee.com/WebServices/WebService1/upload.asmx.cs 里面的代码(我的网站实际上并不叫MySite)

[WebService(Namespace = "http://MySite.somee.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public void WriteFile(byte[] buffer, string FileName)
{            
StreamWriter sw = new StreamWriter(FileName, false);
sw.Write(buffer.ToString());
sw.Close();
}
}

我做错了什么?

编辑:我将我的网络服务代码更改为如下所示

    [WebMethod]
    public void UploadFile(byte[] f, string fileName)
    {
            MemoryStream ms = new MemoryStream(f);
            FileStream fs = new FileStream(Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "/ciao.txt");, FileMode.Create)
            ms.WriteTo(fs);
            ms.Close();
            fs.Close();
            fs.Dispose();
    }

并相应地更新了我的客户

                FileInfo fInfo = new FileInfo("ciao.txt");
                FileStream fStream = new FileStream("ciao.txt", FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fStream);
                long numBytes = fInfo.Length;
                byte[] data = br.ReadBytes((int)numBytes);
                br.Close();
                MyService.UploadFile(data, "ciao.txt");
                fStream.Close();
                fStream.Dispose();

这样我不会得到任何异常但文件仍然没有创建,我在我的网站上寻找“ciao.txt”但找不到它。

有什么帮助吗?

edit2:解决了!我的网站设置在框架 4.0 - 4.5 上,而我的程序是在框架 3.5 下编译的,只要我切换它工作的框架。

【问题讨论】:

  • 你试过调试这个吗? StreamWriter 构造函数之前的FileName 的值是多少?此外,StreamWriter 写入字符串,而您正在向其传递字节。您需要直接尝试FileStream
  • 问题是我没有在本地运行它,它托管在某个虚拟服务器上,我只能调试客户端应用程序。 @CSharp Student 是的,我知道,我的意思是应该使用什么字符串 @ ?文件名?
  • @CSharpStudent 你在说什么?你知道@ 是什么意思吗?

标签: c# file upload asmx


【解决方案1】:

我发现这篇文章有助于将文件从网站加载到数据库中:

http://www.codeproject.com/Articles/48619/Reading-and-Writing-BLOB-Data-to-Microsoft-SQL-or

【讨论】:

    【解决方案2】:

    您的问题可能与此类似,并且已经得到解答:

    "The given path's format is not supported."

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多