通过web服务上载文件:不支持给定的文件路径格式



我正在尝试使用web服务将文件上传到web服务器。问题是,每次尝试使用引用的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();
}
}

我做错了什么?

编辑:我更改了我的web服务代码,使其看起来像这个

    [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下编译的,当我切换框架时,它就工作了。

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

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

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

"给定的路径';不支持s格式"

最新更新