我需要将文件与来自Windows Phone的xml字符串一起发送到服务器。
下面的是服务器端IhttpHandler代码。
现在请给我一些Windows手机的代码示例,一个大文件可以通过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
}
}
答案 0 :(得分: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 an image