我似乎无法找到解决此问题的方法。我试图在Windows Mobile 6上获得我的Compact Framework应用程序,以便能够将其本地文件系统上的文件移动到另一个系统。
以下是我所知道的解决方案:
FTP - 大多数问题都存在 API使用起来很昂贵。
HTTP PUT - 据我所知,我不能在IIS7上使用匿名PUT,而这是系统运行的Web服务器。 (一种极端的解决方法是使用不同的Web服务器来输出文件,并让其他系统将其传输到IIS系统)。
Windows共享 - 我需要对共享进行身份验证,而我还没有看到通过Windows Mobile传递此身份验证的方法。
最后一种方法是要求设备被用来传输这些文件,但我真的喜欢能够无线传输这些文件。
答案 0 :(得分:1)
FTP:定义“太贵”。你的意思是性能或字节开销或美元成本? Here's a free one with source
HTTP:IIS7当然支持托管web services或custom IHttpHandlers。您可以非常轻松地使用其中一个进行数据上传。
Windows共享只需要您P/Invoke the WNet APIs to map the share,但它并不是非常复杂。
答案 1 :(得分:1)
我最终只是通过PHP脚本将信息传递给Web服务器。
上面提供的选项对我的情况不起作用。
这是它的要点。我在那里有一些代码,里面有进度条和各种检查和处理程序,与简单地发送文件无关,但我相信你可以选择它。我已经从C#和PHP中删除了我的身份验证代码,但是如果有必要的话,推送自己的身份验证代码应该不会太难。
在C#中:
/*
* Here's the short+sweet about how I'm doing this
* 1) Copy the file from mobile device to web server by querying PHP script with paramaters for each line
* 2) PHP script checks 1) If we got the whole data file 2) If this is a duplicate data file
* 3) If it is a duplicate, or we didn't get the whole thing, it goes away. The mobile
* device will hang on to it's data file in the first case (if it's duplicate it deletes it)
* to be tried again later
* 4) The server will then process the data files using a scheduled task/cron job at an appropriate time
*/
private void process_attempts()
{
Uri CheckUrl = new Uri("http://path/to/php/script?action=check");
WebRequest checkReq = WebRequest.Create(CheckUrl);
try
{
WebResponse CheckResp = checkReq.GetResponse();
CheckResp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
}
StreamReader dataReader = File.OpenText(datafile);
String line = null;
line = dataReader.ReadLine();
while (line != null)
{
Uri Url = new Uri("http://path/to/php/script?action=process&line=" + line);
WebRequest WebReq = WebRequest.Create(Url);
try
{
WebResponse Resp = WebReq.GetResponse();
Resp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
return;
}
try
{
process_bar.Invoke(new SetInt(SetBarValue), new object[] { processed });
}
catch { }
process_num.Invoke(new SetString(SetNumValue), new object[] { processed + "/" + attempts });
processed++;
line = dataReader.ReadLine();
}
dataReader.Close();
Uri Url2 = new Uri("http://path/to/php/script?action=finalize&lines=" + attempts);
Boolean finalized = false;
WebRequest WebReq2 = WebRequest.Create(Url2);
try
{
WebResponse Resp = WebReq2.GetResponse();
Resp.Close();
finalized = true;
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
finalized = false;
}
MessageBox.Show("Done!");
this.Invoke(new Close(closeme));
}
在PHP中(为了您的利益而彻底评论!):
<?php
//Get the GET'd values from the C#
//The current line being processed
$line = $_GET['line'];
//Which action we are doing
$action = $_GET['action'];
//# of lines in the source file
$totalLines = $_GET['lines'];
//If we are processing the line, open the data file, and append this new line and a newline.
if($action == "process"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//open the file
$fh = fopen($dataFile, 'a');
//Write the line, and a newline to the file
fwrite($fh, $line."\r\n");
//Close the file
fclose($fh);
//Exit the script
exit();
}
//If we are done processing the original file from the C# application, make sure the number of lines in the new file matches that in the
//file we are transferring. An expansion of this could be to compare some kind of hash function value of both files...
if($action == "finalize"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//Count the number of lines in the new file
$lines = count(file($dataFile));
//If the new file and the old file have the same number of lines...
if($lines == $totalLines){
//File has the matching number of lines, good enough for me over TCP.
//We should move or rename this file.
}else{
//File does NOT have the same number of lines as the source file.
}
exit();
}
if($action == "check"){
//If a file with this unique file name already exists, delete it.
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
if(file_exists($dataFile)){
unlink($dataFile);
}
}
?>