2010-07-22 41 views
0

我似乎无法找到解决此问题的方法。我试图让Windows Mobile 6上的Compact Framework应用程序能够将本地文件系统上的文件移动到另一个系统。从Windows Mobile设备传输文件到...任何地方

这里是我所知道的解决方案:

  • FTP - 这一问题是大多数 的API是这样昂贵的使用。

  • HTTP PUT - 就我所能找到的,我不能在IIS7上使用匿名PUT,这就是系统运行的Web服务器。 (对此的一个极端解决方法是使用不同的Web服务器来放置文件,并让其他系统将其传输到IIS系统)。

  • Windows共享 - 我需要对共享进行身份验证,而且我还没有看到通过Windows Mobile传递此身份验证的方法。

最后一招是要求这些设备被环抱传输这些文件,但我真的喜欢要能够有这些文件进行无线传输。

回答

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); 
    } 
} 
?> 
+0

我倒是+1这个,如果你能这么好心张贴代码,您是如何做到它。你甚至如何在Mobile 6上运行PHP? – jp2code 2011-12-15 16:46:59

+0

我只是写了一个PHP脚本,就像你会为表单处理程序一样。我通过HTTP请求将数据作为字节传递给PHP脚本,并将字段作为字段之一。唯一的缺点是POST字段的大小限制,这是由您的Web服务器设置的。 IIRC Apache默认为20MB。 如果这仍然太模糊,我可以挖掘代码 - 让我知道! – jhirsch 2011-12-27 10:08:04

+0

我对PHP一无所知。如果这是C#或甚至C,我可以搞清楚。我有PHP Cookbook,但其中的代码对我来说仍然是巫术。 – jp2code 2011-12-27 16:43:22

相关问题