Upload file to Google Drive directly from Folder (Directory) without using FileUpload control in ASP.Net

Last Reply on Jan 12, 2016 10:52 PM By sonal.shah

Posted on Jan 12, 2016 02:27 AM

i want to know how to upload local file to google drive without using file upload or html input file control.. i have file path and other information

i m using web application and vb language

Posted on Jan 12, 2016 05:49 AM Modified on on Jan 12, 2016 05:52 AM

Please refer below code.

C#

protected void UploadFile(object sender, EventArgs e)
{
    string filePath = "E:\InventoryStockReport.pdf";
    byte[] fileData = File.ReadAllBytes(filePath);
    string fileName = Path.GetFileName(filePath);
    Session["File"] = ConstructHttpPostedFile(fileData, fileName); //If you want you can pass Content Type
    Session["Description"] = txtDescription.Text;
    GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}

public HttpPostedFile ConstructHttpPostedFile(byte[] data, string filename, string contentType = null)
{
    // Get the System.Web assembly reference
    Assembly systemWebAssembly = typeof(HttpPostedFileBase).Assembly;
    // Get the types of the two internal types we need
    Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
    Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");

    // Prepare the signatures of the constructors we want.
    Type[] uploadedParams = { typeof(int), typeof(int) };
    Type[] streamParams = { typeHttpRawUploadedContent, typeof(int), typeof(int) };
    Type[] parameters = { typeof(string), typeof(string), typeHttpInputStream };

    // Create an HttpRawUploadedContent instance
    object uploadedContent = typeHttpRawUploadedContent
        .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
        .Invoke(new object[] { data.Length, data.Length });

    // Call the AddBytes method
    typeHttpRawUploadedContent
        .GetMethod("AddBytes", BindingFlags.NonPublic | BindingFlags.Instance)
        .Invoke(uploadedContent, new object[] { data, 0, data.Length });

    // This is necessary if you will be using the returned content (ie to Save)
    typeHttpRawUploadedContent
        .GetMethod("DoneAddingBytes", BindingFlags.NonPublic | BindingFlags.Instance)
        .Invoke(uploadedContent, null);

    // Create an HttpInputStream instance
    object stream = (Stream)typeHttpInputStream
        .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
        .Invoke(new object[] { uploadedContent, 0, data.Length });

    // Create an HttpPostedFile instance
    HttpPostedFile postedFile = (HttpPostedFile)typeof(HttpPostedFile)
        .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, parameters, null)
        .Invoke(new object[] { filename, contentType, stream });

    return postedFile;
}

Note: If you are using framework 4.5 refer below code to get contentType

C#

string contentType = MimeMapping.GetMimeMapping("someFileName.pdf")