c# - 使用 C# 在 ASP.Net 中使用 Google Drive API 将文件上传到 Google Drive

标签 c# asp.net google-drive-api google-oauth

使用 C# 在 ASP.Net 中使用 Google Drive API 将文件上传到 Google Drive。 最初我已经完成了使用谷歌创建 API 并获取客户端 ID 和客户端密码所需的所有步骤。 然后使用这段代码。

protected void Page_Load(object sender, EventArgs e)
{
    connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    con = new SqlConnection(connStr);
    cm = new ClassMain();
    cmd = new SqlCommand();
    google();
}
protected void UploadFile(object sender, EventArgs e)
{
    Session["File"] = FileUpload1.PostedFile;
    GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");

}
protected void google()
{
    GoogleConnect.ClientId = "<Client ID>";
    GoogleConnect.ClientSecret = "<Client Secret>";
    GoogleConnect.RedirectUri = Request.Url.AbsoluteUri.Split('?')[0];
    GoogleConnect.API = EnumAPI.Drive;
    if (!string.IsNullOrEmpty(Request.QueryString["code"]))
    {
        string code = Request.QueryString["code"];
        string json = GoogleConnect.PostFile(code, (HttpPostedFile)Session["File"], "");
        GoogleDriveFile file = (new JavaScriptSerializer()).Deserialize<GoogleDriveFile>(json);           
    }
    if (Request.QueryString["error"] == "access_denied")
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
    }
}

对于 1 到 10 MB 的小文件,它会正常工作,如果文件大小增加,则给出错误-

网页在http://localhost:1762/ChangeADCUploadedNewded/BlankPage.aspx?code=4%2fAk03HmpOQsV5Na_pM2fs5vzdfJTa2yoMzWAy-lD_CVE可能会暂时关闭,也可能已永久移动到新的网址。

解决错误。

此时未出现 403-Invalid Credential 错误。

最佳答案

我做了一些调查,发现 Google 曾经有 10MB 的上传限制,您可以在这里看到:https://productforums.google.com/forum/#!topic/drive/N9Ruvnf0Jx8但他们去年将该限制提高到 1TB。对于大于 10MB 的文件,我能找到的唯一限制是当上传的文件需要转换为 Google 文档、表格或幻灯片时。

关于c# - 使用 C# 在 ASP.Net 中使用 Google Drive API 将文件上传到 Google Drive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113219/

相关文章:

c# - 如何检查 UI 元素是否已到达 Windows Phone 中的页面顶部

c# - 当我调用 ThrowIfCancellationRequested() 时,OperationCanceledException 未被用户代码处理

c# - 当请求 xml 页面时提供 ashx 页面

c# - 使用数据属性使 web api 以键值作为键序列化字典

Java Google Drive API 没有客户端 secret 或新项目

c# - 使用 TwoWay 将 string[] 绑定(bind)到 XAML

c# - MVC 5 中的身份验证过滤器

rest - 通过 REST v3 上传的文件显示为 "Untitled"

curl - 如何通过curl或wget下载Google Drive URL

c# - 是否可以从 DetailsView 中的 CheckboxField 中提取 Checked 状态?