2011-08-08 89 views
3

我正在爲windows phone 7開發應用程序。從這裏我想上傳一個圖像文件到遠程server.I使用以下代碼接收文件在我的接收端:在windows phone 7應用程序中發佈圖像文件

if (Request.Files.Count > 0) 
{ 
    string UserName = Request.QueryString["SomeString"].ToString(); 
    HttpFileCollection MyFilecollection = Request.Files;   
    string ImageName = System.Guid.NewGuid().ToString() + MyFilecollection[0].FileName; 
    MyFilecollection[0].SaveAs(Server.MapPath("~/Images/" + ImageName)); 
} 

現在我的問題是,如何從(使用PhotoChooserTask)。我曾嘗試下面的代碼,但沒有成功我的Windows Phone 7平臺發佈的文件(以下代碼從PhotoChooserTask叫完成事件處理程序

private void UploadFile(string fileName, Stream data) 
{ 
    char[] ch=new char[1]; 
    ch[0] = '\\'; 
    string [] flname=fileName.Split(ch); 

    UriBuilder ub = new UriBuilder("http://www.Mywebsite.com?SomeString="+ussi); 
    ub.Query = string.Format("name={0}", flname[6]); 
    WebClient c = new WebClient(); 
    c.OpenWriteCompleted += (sender, e) => 
    { 
     PushData(data, e.Result); 
     e.Result.Close(); 
     data.Close(); 
    }; 
    c.OpenWriteAsync(ub.Uri); 
} 

private void PushData(Stream input, Stream output) 
{ 
    byte[] buffer = new byte[4096]; 
    int bytesRead; 
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0) 
    { 
     output.Write(buffer, 0, bytesRead); 
    } 
} 

請幫我拿到擺脫這個問題。 謝謝

回答

2

我能夠得到這個工作,但沒有使用Files集合。從我的帖子在http://chriskoenig.net/2011/08/19/upload-files-from-windows-phone/

客戶端代碼

public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void SelectButton_Click(object sender, RoutedEventArgs e) 
    { 
     PhotoChooserTask task = new PhotoChooserTask(); 
     task.Completed += task_Completed; 
     task.Show(); 
    } 

    private void task_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult != TaskResult.OK) 
      return; 

     const int BLOCK_SIZE = 4096; 

     Uri uri = new Uri("http://localhost:4223/File/Upload", UriKind.Absolute); 

     WebClient wc = new WebClient(); 
     wc.AllowReadStreamBuffering = true; 
     wc.AllowWriteStreamBuffering = true; 

     // what to do when write stream is open 
     wc.OpenWriteCompleted += (s, args) => 
     { 
      using (BinaryReader br = new BinaryReader(e.ChosenPhoto)) 
      { 
       using (BinaryWriter bw = new BinaryWriter(args.Result)) 
       { 
        long bCount = 0; 
        long fileSize = e.ChosenPhoto.Length; 
        byte[] bytes = new byte[BLOCK_SIZE]; 
        do 
        { 
         bytes = br.ReadBytes(BLOCK_SIZE); 
         bCount += bytes.Length; 
         bw.Write(bytes); 
        } while (bCount < fileSize); 
       } 
      } 
     }; 

     // what to do when writing is complete 
     wc.WriteStreamClosed += (s, args) => 
     { 
      MessageBox.Show("Send Complete"); 
     }; 

     // Write to the WebClient 
     wc.OpenWriteAsync(uri, "POST"); 
    } 
} 

服務器代碼

public class FileController : Controller 
{ 
    [HttpPost] 
    public ActionResult Upload() 
    { 
     string filename = Server.MapPath("/Uploads/" + Path.GetRandomFileName(); 
     try 
     { 
      using (FileStream fs = new FileStream(filename), FileMode.Create)) 
      { 
       using (BinaryWriter bw = new BinaryWriter(fs)) 
       { 
        using (BinaryReader br = new BinaryReader(Request.InputStream)) 
        { 
         long bCount = 0; 
         long fileSize = br.BaseStream.Length; 
         const int BLOCK_SIZE = 4096; 
         byte[] bytes = new byte[BLOCK_SIZE]; 
         do 
         { 
          bytes = br.ReadBytes(BLOCK_SIZE); 
          bCount += bytes.Length; 
          bw.Write(bytes); 
         } while (bCount < fileSize); 
        } 
       } 
      } 

      return Json(new { Result = "Complete" }); 
     } 
     catch (Exception ex) 
     { 
      return Json(new { Result = "Error", Message = ex.Message }); 
     } 
    } 
} 

請注意,我使用ASP.NET MVC接受我的文件,但你應該能夠使用與WebForms相同的核心邏輯。

/chris

相關問題