从xamarin上传文件到Web api服务器 - 请求处理时间太长

时间:2016-05-04 23:59:42

标签: c# file-upload asp.net-web-api xamarin

我使用HttpClient将二进制文件发布到Azure托管的服务器。对于那些规模不大的文件,请求需要很长时间才能获得。有时,当客户端因超时而取消任务时,服务器会收到请求。我使用以下代码异步上传数据:

  public partial class Form1 : Form
{
    private const double CHOCOLATE_CHIP = 8.98;
    private const double OATMEAL = 6.98;
    private const double VANILLA_WAFER = 6.48;
    private double price;


    public Form1()
    {
        InitializeComponent();
        this.listBox1.Items.AddRange(new object[] { 0.5, 1.0, 2.0, 3.0 });
    }


    private void close_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void clear_Click(object sender, EventArgs e)
    {
        accumulated.Clear();
    }

    private void buy_Click(object sender, EventArgs e)
    {

    }

    private void chocolateChip_CheckedChanged(object sender, EventArgs e)
    {
        if(chocolateChip.Checked == true)
        {
            if (listBox1.SelectedItem != null)

                price = CHOCOLATE_CHIP * ((double)listBox1.SelectedItem * 0.50);

              accumulated.Text(price.ToString));
        }
    }
}
}

服务器端代码看起来像这样(为了简洁,我留下了一些代码):

public async Task<HttpResponseMessage> UploadFile(byte[] file)
{
    videoThumbName = Guid.NewGuid().ToString();
    var progress = new System.Net.Http.Handlers.ProgressMessageHandler();

    progress.HttpSendProgress += progress_HttpSendProgress;

    using (var client = HttpClientFactory.Create(progress))
    {
        client.BaseAddress = new Uri(GlobalVariables.host);

        // Set the Accept header for BSON.
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/bson"));

        var request = new uploadFileModel { data = file, dateCreated = DateTime.Now, fileName = fileName, username = loggedUser, VideoThumbName = videoThumbName};

        // POST using the BSON formatter.
        MediaTypeFormatter bsonFormatter = new BsonMediaTypeFormatter();
        var m = client.MaxResponseContentBufferSize;
        var result = await client.PostAsync("api/media/upload", request, bsonFormatter);

        return result.EnsureSuccessStatusCode();
    }
}

由于我的互联网连接请求可能会迟到吗?我发送的文件即使大小也不大,它们的大小相当于1-2 mbs。我想知道更大的文件需要多长时间。 无论如何我可以改进这个过程吗?

0 个答案:

没有答案