Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone 8提交带有图像的post表单_C#_Html_Wpf_Windows Runtime - Fatal编程技术网

C# Windows Phone 8提交带有图像的post表单

C# Windows Phone 8提交带有图像的post表单,c#,html,wpf,windows-runtime,C#,Html,Wpf,Windows Runtime,我正在Windows phone 8.0上工作,在尝试从手机向网站提交帖子请求时遇到问题。输入名为“file”,它只接受图像文件 <form action="upmeme" method="post" enctype="multipart/form-data"> <input type="file" class="file" name="file" id="file"><br> <input type="submit" class="subm

我正在Windows phone 8.0上工作,在尝试从手机向网站提交帖子请求时遇到问题。输入名为“file”,它只接受图像文件

<form action="upmeme" method="post" enctype="multipart/form-data">
   <input type="file" class="file" name="file" id="file"><br>
   <input type="submit" class="submit" name="submit" value="Submit">
</form>

我试着在互联网上四处搜索,结果和我的一样。我的代码哪里出错了吗?

检索位图图像:

public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    if (args.Files.Count > 0)
    {
        var imageFile = args.Files[0] as StorageFile;
        // Ensure the stream is disposed once the image is loaded
        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = new BitmapImage();

            await bitmapImage.SetSourceAsync(fileStream);
            ImageControl.Source = bitmapImage;

            await _viewModel.Upload(imageFile);
        }               
    }
}
internal async Task Upload(Windows.Storage.StorageFile file)
{
    var fileStream = await file.OpenAsync(FileAccessMode.Read);
    fileStream.Seek(0);

    var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
    await reader.LoadAsync((uint)fileStream.Size);

    Globals.MemberId = ApplicationData.Current.LocalSettings.Values[Globals.PROFILE_KEY];
    var userName = "Rico";
    var sex = 1;
    var url = string.Format("{0}{1}?memberid={2}&name={3}&sex={4}", Globals.URL_PREFIX, "api/Images", Globals.MemberId, userName,sex);
    byte[] image = new byte[fileStream.Size];

    await UploadImage(image, url);
}
public async Task UploadImage(byte[] image, string url)
{
    Stream stream = new System.IO.MemoryStream(image);
    HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

    Uri resourceAddress = null;
    Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
    Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
    request.Content = streamContent;

    var httpClient = new Windows.Web.Http.HttpClient();
    var cts = new CancellationTokenSource();
    Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
}
创建文件流:

public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    if (args.Files.Count > 0)
    {
        var imageFile = args.Files[0] as StorageFile;
        // Ensure the stream is disposed once the image is loaded
        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = new BitmapImage();

            await bitmapImage.SetSourceAsync(fileStream);
            ImageControl.Source = bitmapImage;

            await _viewModel.Upload(imageFile);
        }               
    }
}
internal async Task Upload(Windows.Storage.StorageFile file)
{
    var fileStream = await file.OpenAsync(FileAccessMode.Read);
    fileStream.Seek(0);

    var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
    await reader.LoadAsync((uint)fileStream.Size);

    Globals.MemberId = ApplicationData.Current.LocalSettings.Values[Globals.PROFILE_KEY];
    var userName = "Rico";
    var sex = 1;
    var url = string.Format("{0}{1}?memberid={2}&name={3}&sex={4}", Globals.URL_PREFIX, "api/Images", Globals.MemberId, userName,sex);
    byte[] image = new byte[fileStream.Size];

    await UploadImage(image, url);
}
public async Task UploadImage(byte[] image, string url)
{
    Stream stream = new System.IO.MemoryStream(image);
    HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

    Uri resourceAddress = null;
    Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
    Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
    request.Content = streamContent;

    var httpClient = new Windows.Web.Http.HttpClient();
    var cts = new CancellationTokenSource();
    Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
}
从图像创建内存流:

public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
    if (args.Files.Count > 0)
    {
        var imageFile = args.Files[0] as StorageFile;
        // Ensure the stream is disposed once the image is loaded
        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
        {
            // Set the image source to the selected bitmap
            BitmapImage bitmapImage = new BitmapImage();

            await bitmapImage.SetSourceAsync(fileStream);
            ImageControl.Source = bitmapImage;

            await _viewModel.Upload(imageFile);
        }               
    }
}
internal async Task Upload(Windows.Storage.StorageFile file)
{
    var fileStream = await file.OpenAsync(FileAccessMode.Read);
    fileStream.Seek(0);

    var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));
    await reader.LoadAsync((uint)fileStream.Size);

    Globals.MemberId = ApplicationData.Current.LocalSettings.Values[Globals.PROFILE_KEY];
    var userName = "Rico";
    var sex = 1;
    var url = string.Format("{0}{1}?memberid={2}&name={3}&sex={4}", Globals.URL_PREFIX, "api/Images", Globals.MemberId, userName,sex);
    byte[] image = new byte[fileStream.Size];

    await UploadImage(image, url);
}
public async Task UploadImage(byte[] image, string url)
{
    Stream stream = new System.IO.MemoryStream(image);
    HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());

    Uri resourceAddress = null;
    Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceAddress);
    Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Post, resourceAddress);
    request.Content = streamContent;

    var httpClient = new Windows.Web.Http.HttpClient();
    var cts = new CancellationTokenSource();
    Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request).AsTask(cts.Token);
}

这个版本用于Windows phone 8.1,是吗?我正在使用8.0,我认为它不支持您的脚本,无论如何,感谢您的anwser。我已经实现了一些用于上传文件的简单HTTP类,请看“HTTP POST”一章