views:

679

answers:

2

I need to upload a file using C# from a windows mobile app to a website. It's running PHP as the webservice on the other side, though I guess it really doesn't have to if there's another way to be able to get the file up there. There is no server-side ASP support, however. My problem really isn't the PHP, it's the mobile C# code.

Also, System.net.WebClient does NOT exist in the compact framework, so unfortunately, that simple solution is gone.

Let me apologize in advance, because I know this is a relatively commonly asked question, but I just can't seem to find an answer. I've spent an unseemly amount of time on this one particular problem with no solution, so any help at all would be greatly greatly appreciated. Thanks a lot!

+2  A: 

Have a look at this article Improving .NET Compact Framework HTTP Communications using HttpWebRequest and Custom ASP.NET Providers in MSDN. It is specific for ASP technology, but the Compact Framework remains the same. Basically it uses an HttpWebRequest with a PUT method. If you aren't allowed to do so in the server, you need to create a sequence of POST requests yourself and handle them accordingly.

kgiannakakis
A: 

Hi,

The following code snippet may help solve the problem we have been discussing here. Please ignore this if it irrelevant to this topic.

Thanks,

Regards, Igor Stevebin

A brief description of how to use the article or code. The class names, the methods and properties, any tricks or tips.

Blocks of code should be set as style "Formatted" like this:

Sample Code

using System.Data; using System.Data.Sql using System.Data.SqlClient; using System.Web; using System.Web.Services;

public class FileUploader: System.Web.Services.WebService { SqlConnection myConnection = new SqlConnection("Data Source=server name ;Initial Catalog=database name; User ID=username; Password='password';"); SqlCommand myCommand = new SqlCommand(); string queryString = "";

public string UploadFile(byte[] f, string fileName) { // the byte array argument contains the content of the file // the string argument contains the name and extension // of the file passed in the byte array

string nm = data[0]; string sn =data[1]; string bn =data[2]; string st = data[3]; byte img = Convert.Tobyte(img); myConnection.Open(); queryString = "INSERT INTO tablename(Name,SchemeName,BeneficiarName,Status,Photo)"

  • "VALUES('" + nm + "','" + sn + "','"+ bn +"','" + st + "',@img,'")";

myCommand.Parameters.AddWithValue("@img",f); myCommand.Connection = myConnection; myCommand.CommandType = CommandType.Text; myCommand.CommandText = queryString; int res = myCommand.ExecuteNonQuery(); myConnection.Close();

if (res > 0) { strres = "File Uploaded successfully"; }

else { strres = "File not uploaded"; } return strres; }

Requirements

Visual Studio.Net 2005

.Net Framework 2.0

MS SQL Server 2000 database or MS SQL Server 2005 database.

Igor Stevebin