使用 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/