HTTP客户端上传文件C#上传文件、客户端、HTTP

2023-09-03 14:55:25 作者:有生之年拥你入眠

这是在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();
        }
    }
怎么使用百度网盘客户端上传文件

现在效果很好

 
精彩推荐