Can i use an web service like this?

trying to save an .txt file with an web service.

Page 1 of 1

8 Replies - 1465 Views - Last Post: 15 January 2010 - 04:33 AM Rate Topic: -----

#1 micke   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Can i use an web service like this?

Post icon  Posted 12 January 2010 - 11:43 AM

Hi,

Is it possible to use an web service like this: http://localhost/web...=f...:\file.txt


PutFile is a Method that saves the file that are located on c:\file.txt


		[WebMethod]
		public void PutFile(byte[] buffer, string filename)
		{
			BinaryWriter binWriter = new BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew, FileAccess.ReadWrite));
			binWriter.Write(buffer);
			binWriter.Close();

		}


or is there another way, and not using any forms

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Can i use an web service like this?

#2 aabycool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-October 09

Re: Can i use an web service like this?

Posted 12 January 2010 - 03:20 PM

Will you please elaborate more what you are trying to do?

You are not using Form then is user going to invoke this webmethod manually?
If yes then what contents it's writing to that txt file?

Was This Post Helpful? 0
  • +
  • -

#3 finaiized   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Can i use an web service like this?

Posted 12 January 2010 - 04:53 PM

You didn't actually tell us anything... maybe try it and see if it works like that. Since that's pretty much what you are asking.
Was This Post Helpful? 0
  • +
  • -

#4 micke   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Re: Can i use an web service like this?

Posted 13 January 2010 - 01:39 AM

Sorry for bad explenation.

What i'm trying to do is to send (file transfer) an .txt file (or any file) with an web service using http post, and then save it on local web server.

And then my question was if it is possible for the end user just to point to the url and add the path to the file they are sending in the url, or do i need to use an Form with an action pointed to the webservice?
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Can i use an web service like this?

Posted 13 January 2010 - 05:53 AM

View Postmicke, on 13 Jan, 2010 - 04:39 AM, said:

Sorry for bad explenation.

What i'm trying to do is to send (file transfer) an .txt file (or any file) with an web service using http post, and then save it on local web server.

And then my question was if it is possible for the end user just to point to the url and add the path to the file they are sending in the url, or do i need to use an Form with an action pointed to the webservice?


have you tried it to see if it works?
Was This Post Helpful? 0
  • +
  • -

#6 micke   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Re: Can i use an web service like this?

Posted 14 January 2010 - 07:38 AM

Yes i have, here is the code:


	[WebMethod(Description = "Upload a single file to the web server")]
		public string UploadFile(byte[] buffer, string fileName)
		{

			try
			{
				MemoryStream ms = new MemoryStream(buffer);

				FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/") + fileName, FileMode.Create);

				ms.WriteTo(fs);

				ms.Close();
				fs.Close();
				fs.Dispose();

				AppendToLog("UPLOAD", "+ fileName + Upload - OK");

				return "OK";
			}
			catch (Exception ex)
			{
				AppendToErrorLog("UPLOAD", "+ error + - Upload - Failed");

				string error = ex.Message.ToString();

				return error;
			}
		}



and a simple form to test it:

 <form action="http://localhost/FileServer.asmx/UploadFile" method="post" target="_blank">
 
 <input id="File1" type="file" /> <br /><br />	  
 <input type="submit" value="Submit" />
 </form>


but no error, nothing happends.
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Can i use an web service like this?

Posted 14 January 2010 - 08:22 AM

when I try to pass a file chosen from the "file" type input box to a webservice that is expecting a byte[], I get this error message...

"System.ArgumentException: Cannot convert C:\Test\test2.txt to System.Byte."

Looks like you need to convert the file to a byte[] manually. Doesn't look like the control does it for you.
Was This Post Helpful? 0
  • +
  • -

#8 micke   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Re: Can i use an web service like this?

Posted 14 January 2010 - 03:48 PM

Ok, thnak you will try that, but i do not get any errors from the web service, is it possible to debug it?
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Can i use an web service like this?

Posted 15 January 2010 - 04:33 AM

You should be getting an error. Your web service is expecting two parameters, and you aren't giving it any. Add the "name" attribute to the file input and give it the value of "buffer". Then add another textbox that allows the user to type in a filename. Add the "name" attribute to the textbox and give the attribut a value of "fileName".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1