I want upload multiple files. When uploading a single file it works fine. When I try to upload multiple files I get the error below.
"Unable to cast object of type 'System.String' to type 'System.Web.HttpPostedFile'."
protected void UploadPhoto(object sender, EventArgs e)
{
Session["File"] = FileUpload1.PostedFile;
}
multiple file
public void UploadFile()
{
string[] dirs = Directory.GetFiles(@"C:\Users\RNKP74\Desktop\GoogleDrive");
foreach (string dir in dirs)
{
// HttpPostedFile postedFile = Request.Files[dir];
Session["File"] = dir;
}
}
How do I cast this? When I use the code below, the postedFile is null.
HttpPostedFile postedFile = Request.Files[dir];
I'm try to upload files to google drive. Using the upload control it works fine, but now I want to upload all the files. The full code is below.
using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using ASPSnippets.GoogleAPI;
using System.Web.Script.Serialization;
using System.Web;
using System.Collections.Generic;
using System.Net.Http;
using System.Net;
using System.IO;
namespace GoogleDriveAutoUpdate
{
public partial class GoogleDriveSample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GoogleConnect.ClientId = "";
GoogleConnect.ClientSecret = "";
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"], "");
}
if (Request.QueryString["error"] == "access_denied")
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Access denied.')", true);
}
UploadFile();
}
protected void UploadPhoto(object sender, EventArgs e)
{
Session["File"] = FileUpload1.PostedFile;
GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}
public void UploadFile()
{
string[] dirs = Directory.GetFiles(@"C:\Users\RNKP74\Desktop\GoogleDrive");
HttpFileCollection MyFileCollection = Request.Files;
foreach (string dir in dirs)
{
//HttpFileCollection postedFile = Request.Files[dir];
HttpPostedFile file = Request.Files[dir];
Session["File"] = file;
GoogleConnect.Authorize("https://www.googleapis.com/auth/drive.file");
}
}
}
}