.NET CORE upload files to the cloud repository [code to build their own map bed]

.NET CORE upload files to the cloud repository [code to build their own map bed]

First to build a public warehouse (feel free to submit a README file or files to ensure that there is .gitignore master branch), then to gitee personal settings page to find [private] menu to create a token access_token. Gitee official also provides a friendly and debugging based API documentation page swagger of: https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath

Build step

1. Create a file called imagebedwarehouse

  • According to the code cloud normal steps to building a database

2. imagebedCreate a warehouse mastermain branch

  • [Recommended] may submit a README file or files to .gitignore random from a local warehouse
  • Or use your own way is also OK, as long as the warehouse has a masterbranch can

3. Personal Settings page to find [private] tokens generate a new token

  • Find [private] token
    Find [private] token
  • Generating a new token
    Generating a new token

4. Use Gitee official website simple test file upload API documentation

  • Fill in the information
    Fill in the information
  • Click Test
    Click Test
  • Submit records
    Submit records
  • View content
    View content

Instructions

Based on .NET CORE MVC project

    /// <summary>
    /// 码云仓储文件上传API
    /// </summary>
    /// <param name="stream"></param>
    /// <param name="file"></param>
    /// <returns></returns>
    public async Task<(string url, bool success)> UploadGitee(Stream stream, string file)
    {
        string base64String = Convert.ToBase64String(stream.StreamToByte());
        string path = $"{DateTime.Now:yyyyMMdd}/{Path.GetFileName(file)}";
        using (var resp = await _httpClient.PostAsJsonAsync(AppConfig.GiteeConfig.ApiUrl + HttpUtility.UrlEncode(path), new
        {
            access_token = AppConfig.GiteeConfig.AccessToken,
            content = base64String,
            message = "上传一个文件"
        }))
        {
            if (resp.IsSuccessStatusCode || (await resp.Content.ReadAsStringAsync()).Contains("already exists"))
            {
                return (AppConfig.GiteeConfig.RawUrl + path, true);
            }
        }

        return await Task.Run(() => (null, false));
    }

    /// <summary>
    /// MVC上传文件
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("upload"), ApiExplorerSettings(IgnoreApi = false)]
    public async Task<ActionResult> UploadFile(IFormFile file)
    {
        var (url, success) = await _imagebedClient.UploadImage(file.OpenReadStream(), file.FileName);
        return await success ? Json(new { code = 1, msg = "success", data = url }) : Json(new { code = 0, msg = "failure" });            
    }

The full story: https://github.com/Run2948/ImageBedDemo

Guess you like

Origin www.cnblogs.com/Run2948/p/10990044.html