0

我需要将文件与来自 windows phone 的 xml 字符串一起发送到服务器。

下面是服务器端 IhttpHandler 代码。

现在请给我一些 Windows Phone 的代码示例,1 个大文件可以上传到服务器,还可以通过 POST 上传 xml 或任何字符串?

在服务器端代码下方

<%@ WebHandler Language="C#" Class="Test" %>

using System;
using System.Web;

using System.IO;


public class Test : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "text/plain";



    context.Response.Write(AddSurveyMediaWithFile(context.Request.Files[0] , context.Request["MediaDataXML"].ToString()));

}

public bool IsReusable {
    get {
        return false;
    }
}

public string ShowMessage(string msg)
{
    return msg;
}

public String AddSurveyMediaWithFile(HttpPostedFile uploadedFile, string MediaDataXML)
{



    try
    {
        Survey.AddDataMediaXMLCheck(MediaDataXML, "Media");


        string FilePath = HttpContext.Current.Server.MapPath("../MediaFiles/");
        AttributeValue objAttributeValue = ParseValue(MediaDataXML);
        objAttributeValue.Value = objAttributeValue.Value;
        OrganizationUnitLicense objLicense = OrganizationUnitLicense.ValidateFilesize(objAttributeValue.UserWorkUnitSurveyID);

        if (objAttributeValue.Filesize > 0)
        {


            if (objLicense.Space > 0)
            {

                if ((objLicense.TotalFilesize + objAttributeValue.Filesize) <= (objLicense.Space * 1024 * 1024))
                {
                    string msg = Survey.AddSurveyData(MediaDataXML);

                    if (msg == Enumerators.SQLReturn.SUCCESS.ToString())
                    {
                        if (File.Exists(FilePath + objAttributeValue.Value))
                            File.Delete(FilePath + objAttributeValue.Value);


                        uploadedFile.SaveAs(FilePath + objAttributeValue.Value);



                        return Enumerators.SQLReturn.SUCCESS.ToString();
                    }
                    else
                    {
                        return msg;
                    }

                }
                else
                {

                    return "FILESIZE_" + Enumerators.SQLReturn.LIMIT_NOT_EXIST.ToString();
                }
            }
            else
            {
                string msg = Survey.AddSurveyData(MediaDataXML);

                if (msg == Enumerators.SQLReturn.SUCCESS.ToString())
                {
                    if (File.Exists(FilePath + objAttributeValue.Value))
                        File.Delete(FilePath + objAttributeValue.Value);

                    uploadedFile.SaveAs(FilePath + objAttributeValue.Value);


                    return Enumerators.SQLReturn.SUCCESS.ToString();
                }
                else
                {
                    return msg;
                }
            }
        }
        else
        {
            return "FILESIZE_ZERO";
        }
    }
    catch (Exception ex)
    {



        return ex.Message + Environment.NewLine + ex.StackTrace; //Enumerators.SQLReturn.ERROR.ToString();
    }
}

private AttributeValue ParseValue(string MediaDataXML)
{
    .....    //to do section
}

}
4

1 回答 1

0

我没有阅读您的代码,但无论如何我都会回答您的问题。

要将字符串数据上传到服务器:

Public Sub UploadFile(xmlPath As String, type As String)
    Try
        Dim webClient As New WebClient()
        webClient.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"
        Dim uri = New Uri(xmlPath, UriKind.Absolute)
        Dim postData As New StringBuilder()
        postData.AppendFormat("{0}={1}", "YOUR_PARAMETER_HERE", "")

        webClient.Headers(HttpRequestHeader.ContentLength) = postData.Length.ToString()
        AddHandler webClient.UploadStringCompleted, AddressOf webClient_UploadStringCompleted
        webClient.UploadStringAsync(uri, "POST", postData.ToString())
    Catch ex As Exception
        LogError(ex)
    End Try
End Sub

然后你需要在这里处理响应:

 Private Sub webClient_UploadStringCompleted(sender As Object, e As UploadStringCompletedEventArgs)
        Try
            Dim jsonString = e.Result
            //PUT YOUR OWN CODE HERE
        Catch ex As Exception
            LogError(ex)
        End Try
    End Sub

至于上传文件,我建议你阅读这个答案:WP7 - POST form with a image

于 2013-06-30T11:15:43.500 回答