Home > Enterprise >  HTTP CLIENT Upload File C#
HTTP CLIENT Upload File C#

Time:03-07

This is the code below (C#) To upload in https://nft.storage/ It Works fine But when I upload mp4 file (Uploaded successfully) , The uploaded file doesn’t work . Source Code https://github.com/filipepolizel/unity-nft-storage I used many different HTTPCLIENT example but it same broken Uploaded mp4 File: 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;
            }
        }

CodePudding user response:

the answer helped me . thanks , I changed the Update method to :

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();
        }
    }

It works great now

  • Related