.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 imagebed
warehouse
- According to the code cloud normal steps to building a database
2. imagebed
Create a warehouse master
main 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
master
branch can
3. Personal Settings page to find [private] tokens generate a new token
4. Use Gitee official website simple test file upload API documentation
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" });
}