0

我正在尝试从谷歌驱动器获取文件,我正在获取文件内容,将其转换为字节数组并将其发送到后端以将文件保存在服务器上(为此,我使用 Ajax POST 方法)

这就是我想要做的

// 一个简单的回调实现。

function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
        fileId = data.docs[0].id;
        gapi.load('client', function () {
            gapi.client.load('drive', 'v3', function () {
                var file = gapi.client.drive.files.get({ 'fileId': fileId, fields: '*' });
                file.execute(function (resp) {
                    fileext = resp.originalFilename.split('.').pop();
                    inputFileName = randomString(8) + "." + fileext;
                    $('#inputFileName').val(inputFileName);
                    inputFileName = $('#inputFileName').val();
                    fileSize = resp.size;
                    if (fileSize < maxFileSize) {
                        fileId = resp.id;
                        accessToken = oauthToken;
                        var xhr = new XMLHttpRequest();
                        xhr.open("GET", "https://www.googleapis.com/drive/v3/files/" + fileId + '?alt=media', true);
                        xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
                        xhr.responseType = 'arraybuffer';
                        xhr.onreadystatechange = function () {//Call a function when the state changes.
                            if (xhr.readyState == 4 && xhr.status == 200) {
                                base64 = base64ArrayBuffer(xhr.response);
                              $.ajax({
                                type: "POST",
                                  url: "googledrive.aspx/GetDriveFile",
                                  data: JSON.stringify({
                                      FileName: inputFileName, Base64: base64
                                  }),
                                  contentType: "application/json; charset=utf-8",
                                  dataType: "json",
                                success: function (msg) {
                                    if (msg.d == "true") {
                                       alert("Success");
                                    } else {
                                       alert("Failed");
                                    }
                                },
                                error: function (msg) {
                                    alert(msg.d);
                                }
                            });
                            }
                        }
                        xhr.send();
                    } else {
                        alert("Max File Size");
                    }
                });
            });
        });
    }
}

这是我将文件保存到服务器的后端代码

 public static string GetDriveFile(string FileName, string Base64)
    {
        try
        {
            string inputPath = HttpContext.Current.Server.MapPath("UploadedFiles/") + FileName;
            byte[] image64 = Convert.FromBase64String(Base64);
            File.WriteAllBytes(inputPath, image64);
            return "true";
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }

我正在使用here的base64转换

如图所示,Ajax POST 方法未触发并获得 500 内部服务器错误。

在此处输入图像描述

4

0 回答 0