1

How am I going to upload an image (if this method is possible) with this following code :

js :

$.ajax({
    type: "POST",
    url: '@Url.Action("FirstAjax", "TrainingCourse")',
    data: JSON.stringify({file:"C:\\Users\\jmanaban\\Downloads\\image.jpg"}),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFunc,
    error: errorFunc
});

and in my controller :

public ActionResult FirstAjax(String file)
{
    \\do process here
    return Json(file, JsonRequestBehavior.AllowGet);
} 

I'm still a newbie in c# so I'm not so sure if I'm doing the right thing here. I've seen lots of code snippet in the internet but I like to just pursue this one if possible. Thanks experts.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
JanLeeYu
  • 981
  • 2
  • 9
  • 24
  • 1
    JSON.stringify, parse a string as a json objet, but it is not used to read the content of a file. https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/JSON/stringify You are close to the solution. You have many ways to do what you want, you can convert your image to a base64 string, and send this string by ajax, or do a post with content/multipart, or.... – Rumpelstinsk Mar 22 '16 at 06:37
  • I already have the file path but I don't know how to transfer it to my upload folder. What I usually see that they use is something like this : `FirstAjax(HttpPostedFileBase file ... ` – JanLeeYu Mar 22 '16 at 06:48
  • I think I am misunderstood: Are you trying to get the file on server side with the file path on client side? This is impossible. What you have to do is send the content, no the path. – Rumpelstinsk Mar 22 '16 at 07:02
  • yes you are right. I am just sending the path from client to server. – JanLeeYu Mar 22 '16 at 07:05
  • Then you will never be able to get the file on server side. Client side and server side are diferent machines. Imagine for example that I open your website from my mobile phone, and I try to send you and image. I select "//sd/dscim/wallpaper1.jpg". And i send you that string to the server. What do you plan to do with that string? That's useless. That path doesn't exist on the server and your pc cannot access to the files on my mobile. As I tell you before, you need to send the content of the file, no the path on client machine. – Rumpelstinsk Mar 22 '16 at 07:10
  • OK thanks for that one. – JanLeeYu Mar 22 '16 at 07:12
  • If you want to send a string to the server. You must convert your image to a base64 string on client side using javascript: https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL Then on serverside, you have to convert again the string to a file http://stackoverflow.com/questions/7134837/how-do-i-decode-a-base64-encoded-string But it's not the only way to do what you want. If you take a look over internet you will find many workarrounds and examples. Hope it's help :) – Rumpelstinsk Mar 22 '16 at 07:12
  • I think ill just send the content. – JanLeeYu Mar 22 '16 at 07:15

0 Answers0