标签:方法 https turn return 查看内容 提交 src util 简单
先建一个公共仓库(随意提交一个README文件或者.gitignore文件保证master分支的存在),然后到gitee的个人设置页面找到【私人令牌】菜单创建一个access_token。Gitee官方还友好的提供了基于swagger的API文档和调试页面: https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoContentsPath
imagebed
的仓库imagebed
仓库创建master
主分支master
分支即可基于.NET CORE MVC项目实现
/// <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" });
}
标签:方法 https turn return 查看内容 提交 src util 简单
原文地址:https://www.cnblogs.com/Run2948/p/10990044.html