1

I am saving image file on google drive using asp.net mvc and webapi. I want full image path. What is the alternate to get full path?

My method in Webapi:

 private void UploadImage(DriveService service, string imageId)
 {
string FOLDER_NAME = "ProductImages";googleFile.File folder = searchFolder(service,FOLDER_NAME);if(folder == null)
folder = createFolder(service,FOLDER_NAME);       
var folderId = folder.Id;
var fileMetaData = new googleFile.File();
//fileMetaData.Name = Path.GetFileName(imageId);--not getting full path
fileMetaData.Name = imageId;
fileMetaData.MimeType = "image/jpg";
fileMetaData.Description = "Upload Image";
fileMetaData.Parents = new List<string> { folderId };
FilesResource.CreateMediaUpload request;
// imageId = @"D:\Camera\IMG_20130525_220410.jpg";
using (var stream = new FileStream(imageId, FileMode.Create))-- i get wrong path in imageid.due to that i get error
{request = service.Files.Create(fileMetaData, stream, "image/jpg");
 request.Fields = "id";
 var upload = request.UploadAsync();
 var file = request.ResponseBody;}
Mel
  • 5,837
  • 10
  • 37
  • 42
Zain
  • 37
  • 1
  • 6
  • Firstly read this: https://stackoverflow.com/a/4942145/1662459, and update your question – G J May 30 '17 at 17:36
  • Secondly try to change your `FileMode` to `Open`. https://msdn.microsoft.com/en-us/library/system.io.filemode – G J May 30 '17 at 17:39
  • if i change fileMode to open also then also i need image file name with full path.but i am not getting image full path.bcs it is said that for security reasons browsers dont allow to give full path of a file.what is the alternate? Example using (var stream = new FileStream(imageId, FileMode.Create)) -- to get my method work, here in imageId i should get full path of a file with image name. which i am not getting.instead i get wrong path(c://users/IIS Express) with image name.what is the alternate of this.pls help – Zain May 30 '17 at 17:49
  • You need to post the file from client page, here you go https://stackoverflow.com/a/5193851/1662459. Then you can read the file bytes and file name (NOT FULL PATH) – G J May 30 '17 at 18:06
  • i am posting file from client page using mvc.net.i dnt want to save my image in my app folder.i want my images to be saved on google drive. Below is my Mvc.net method public async Task Create(ProductViewModel model,HttpPostedFileBase postedFile) { var response = await productClient.CreateProduct(model, postedFile.FileName); if (response.StatusIsSuccessful) { return RedirectToAction("Index", "Product"); }else { AddResponseErrorsToModelState(response); this.AddNotification(response.ResponseResult, NotificationType.ERROR);}return View(model); } – Zain May 30 '17 at 18:36
  • the above controller method sends filename to webapi and webapi saves image on google drive.but bcs it only send file name and not full path with it so in webapi method(Filestream(imageId,filemode.create)) it gives error.do you know any alternate – Zain May 30 '17 at 18:42
  • I have solved the above issue by passing stream to webapi and in webapi i have read the stream:using (var reader = new StreamReader(postedFile.InputStream)) { // apiModel.Image = reader.ReadAsync(postedFile.InputStream.Length+1); byte[] convertBytes = new byte[postedFile.InputStream.Length + 1]; postedFile.InputStream.Read(convertBytes, 0, convertBytes.Length); apiModel.Image = convertBytes; } – Zain May 31 '17 at 15:37

0 Answers0