【问题标题】:to post image file in windows phone 7 application在 windows phone 7 应用程序中发布图像文件
【发布时间】:2011-08-08 04:01:03
【问题描述】:

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

请帮助我摆脱这个问题。 谢谢

【问题讨论】:

    标签: c# windows-phone-7


    【解决方案1】:

    我能够让它工作,但没有使用 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 一起使用。

    /克里斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多