在Windows phone 7应用程序中发布图像文件

本文关键字:图像 文件 布图像 phone Windows 应用程序 | 更新日期: 2023-09-27 18:04:41

我正在为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));
} 

现在我的问题是,我怎么能从我的windows phone 7平台(使用PhotoChooserTask)发布文件。我已经尝试了以下代码,但没有成功。(下面的代码是从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);
    }
}

请帮助我摆脱这个问题。由于

在Windows phone 7应用程序中发布图像文件

我能够使其工作,但不使用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