1

I am trying to upload files to a web server using a web service. The problem is that I get a "given file format not supported" exception every time I try use the referenced web method.

This is the code inside my application:

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();

And this is the code inside http://MySite.somee.com/WebServices/WebService1/upload.asmx.cs (my site is not actually called 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();
}
}

What am I doing wrong?

edit: i changed my web service code to look like this

    [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();
    }

and updated my client accordingly

                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();

this way i don' t get any exceptions but the file still isn' t created, i looked for "ciao.txt" all over my site and couldnt find it.

Any help ?

edit2: solved ! i had my site set on framework 4.0 - 4.5 while my program was compiled under framework 3.5, as soon as i switched the framework it worked.

user1909612
  • 273
  • 5
  • 14
  • Have you tried debugging this? What's the value of `FileName` before the `StreamWriter` constructor? Also, `StreamWriter` writes strings, and you're passing it bytes. You need to try `FileStream` directly. – John Saunders Feb 15 '13 at 00:18
  • The problem is i am not running this locally, it is hosted on a somee virtual server, i can only debug the client side application. @CSharp Student yes i know, i mean what string should use a @ ? FIleName ? – user1909612 Feb 15 '13 at 00:21
  • @CSharpStudent what are you talking about? Do you know what `@` means? – wRAR Feb 15 '13 at 01:25

2 Answers2

0

Here is an article I found to be helpful in loading files into a database from a website:

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

user8128167
  • 6,929
  • 6
  • 66
  • 79
0

Your problem could be similar to this and its already answered:

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

Community
  • 1
  • 1
Divi
  • 7,621
  • 13
  • 47
  • 63