HTTP客户端上传文件C# [英] HTTP CLIENT Upload File C#

查看:0
本文介绍了HTTP客户端上传文件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在https://nft.storage/中上传的代码(C#),它运行得很好,但当我上传MP4文件(上传成功)时,上传的文件不起作用。源代码https://github.com/filipepolizel/unity-nft-storage 我使用了许多不同的HTTPCLIENT示例,但都是一样的 上传的MP4文件损坏:http://ipfs.io/ipfs/bafybeibt4jqvncw6cuyih27mujbpdmsjl46pykablvravh3qg63vuvcdqy

// nft.storage API endpoint
        private static readonly string nftStorageApiUrl = "https://api.nft.storage/";

        // HTTP client to communicate with nft.storage
        private static readonly HttpClient nftClient = new HttpClient();

        // http client to communicate with IPFS API
        private static readonly HttpClient ipfsClient = new HttpClient();

        // nft.storage API key
        public string apiToken;

void Start()
        {
            nftClient.DefaultRequestHeaders.Add("Accept", "application/json");
            if (apiToken != null)
            {
                nftClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiToken);
            }
            else
            {
                // log in console in case no API key is found during initialization
                Debug.Log("Starting NFT Storage Client without API key, please call 'SetApiToken' method before using class methods.");
            }
        }

public async Task<NFTStorageUploadResponse> UploadDataFromFile(string path)
        {
            StreamReader reader = new StreamReader(path);
            string data = reader.ReadToEnd();
            reader.Close();
            print("Uploading...");
            return await UploadDataFromString(data);
        }


public async Task<NFTStorageUploadResponse> UploadDataFromString(string data)
        {
            string requestUri = nftStorageApiUrl + "/upload";
            string rawResponse = await Upload(requestUri, data);
            NFTStorageUploadResponse parsedResponse = JsonUtility.FromJson<NFTStorageUploadResponse>(rawResponse);
            return parsedResponse;
        }

private async Task<string> Upload(string uri, string paramString)
        {
            try
            {
                using (HttpContent content = new StringContent(paramString))
                {
                    //content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    content.Headers.ContentType = new MediaTypeHeaderValue("*/*");
                    HttpResponseMessage response = await nftClient.PostAsync(uri, content);
                    response.EnsureSuccessStatusCode();
                    Stream responseStream = await response.Content.ReadAsStreamAsync();
                    StreamReader reader = new StreamReader(responseStream);
                    return reader.ReadToEnd();
                }
            }
            catch (HttpRequestException e)
            {
                Debug.Log("HTTP Request Exception: " + e.Message);
                Debug.Log(e);
                return null;
            }
        }

推荐答案

答案对我有帮助。谢谢,我将更新方法更改为:

public static async Task<string> Upload(string uri, string pathFile)
    {

        byte[] bytes = System.IO.File.ReadAllBytes(pathFile);

        using (var content = new ByteArrayContent(bytes))
        {
            content.Headers.ContentType = new MediaTypeHeaderValue("*/*");

            //Send it
            var response = await nftClient.PostAsync(uri, content);
            response.EnsureSuccessStatusCode();
            Stream responseStream = await response.Content.ReadAsStreamAsync();
            StreamReader reader = new StreamReader(responseStream);
            return reader.ReadToEnd();
        }
    }

现在效果很好

这篇关于HTTP客户端上传文件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆