Hello best codeproject members,
I have a problem that i can not resolve. I hope someone can get me on the right 'code' I have implant to my code a 'javascript' (there was already a input file for a url, but i decided to add also a file upload button), wenn i go to the site and 'create a new message' and try to upload the file it gives me a 'image not suported error'. Here is a part of the script;
Thank you
The Div;
<div class="form-group">
<input type="file" class="btn btn-default btn-file" style="width:70%" id="file" name="file" />
<input type="button" value="Upload" class="btn btn-primary uploadbtn" id="uploadimg" name="upladimage" />
<img class="wait hide" id="wait" src="/Content/img/pleasewait .gif" />
You can upload the only image file.
</div>
The Javascript;
$("#imgurl").keyup(
function () {
$("#pmainimg").attr("src", $("#imgurl").val());
});
$(document).on('click', '#uploadimg', function () {
if (window.FormData !== undefined) {
var fileUpload = $("#file").get(0);
var files = fileUpload.files;
var fileData = new FormData();
if ($("#file").val()!='') {
fileData.append(files[0].name, files[0]);
$("#wait").removeClass("hide");
$("#wait").addClass("show");
$.ajax({
url: '/Member/UploadImage',
type: "POST",
contentType: false,
processData: false,
data: fileData,
success: function (result) {
if (result.indexOf("http") > -1) {
$("#imgurl").val(result);
$("#pmainimg").attr("src", $("#imgurl").val());
$("#wait").removeClass("show");
$("#wait").addClass("hide");
}
else {
debugger;
$("#errormsg").html("You can upload the only image file.");
$("#errormsg").css("display", "inline");
$("#errormsg").css("color", "red");
$("#wait").removeClass("show");
$("#wait").addClass("hide");
}
},
error: function (err) {
alert("Image Not Supported");
$("#wait").removeClass("show");
$("#wait").addClass("hide");
}
});
}
else
{
$("#errormsg").html("You have not specified a image file.");
$("#errormsg").css("display", "inline");
$("#errormsg").css("color", "red");
$("#wait").removeClass("show");
$("#wait").addClass("hide");
}
} else {
alert("FormData is not supported.");
}
});
Controler;
{
[UserLoginRequired]
public class MemberController : ClickMSGController
{
public ActionResult CreateMessage()
{
return View();
}
public ExpandoObject CreateMessageObject(string ctatitle, string msgtitle, string msgbody, string imgurl,
string linkurl, string linktxt, string reply1, string reply2, string msgname)
{
dynamic button1 = new ExpandoObject();
button1.type = "web_url";
button1.url = linkurl;
button1.title = linktxt;
dynamic button2 = new ExpandoObject();
button2.type = "postback";
button2.title = ctatitle;
button2.payload = Guid.NewGuid().ToString();
dynamic element = new ExpandoObject();
element.title = msgtitle;
element.image_url = imgurl;
element.subtitle = msgbody;
element.buttons = new List<object> { button1, button2 };
dynamic payload = new ExpandoObject();
payload.template_type = "generic";
payload.elements = new List<object> { element };
dynamic attachment = new ExpandoObject();
attachment.type = "template";
attachment.payload = payload;
dynamic message = new ExpandoObject();
message.attachment = attachment;
dynamic fbmsg = new ExpandoObject();
fbmsg.message = message;
if (!string.IsNullOrEmpty(reply1) && !string.IsNullOrEmpty(reply2))
{
dynamic reply1obj = new ExpandoObject();
reply1obj.content_type = "text";
reply1obj.title = reply1;
reply1obj.payload = reply1;
dynamic reply2obj = new ExpandoObject();
reply2obj.content_type = "text";
reply2obj.title = reply2;
reply2obj.payload = reply2;
fbmsg.message.quick_replies = new List<object> { reply1obj, reply2obj };
}
return fbmsg;
}
public ActionResult GetJson(string ctatitle, string msgtitle, string msgbody, string imgurl,
string linkurl, string linktxt, string reply1, string reply2, string msgname)
{
ExpandoObject msgObject = CreateMessageObject(ctatitle, msgtitle, msgbody, imgurl, linkurl, linktxt, reply1, reply2, msgname);
if (Request.Form["savebtn"] == "Save & Get JSON")
{
AdManager.Ad ad = new AdManager.Ad();
ad.AdName = msgname;
ad.CallToAction = ctatitle;
ad.Image_URL = imgurl;
ad.LinkButtonText = linktxt;
ad.LinkButtonURL = linkurl;
ad.Reply1 = reply1;
ad.Reply2 = reply2;
ad.Subtitle = msgbody;
ad.Title = msgtitle;
ad.UserID = CurrentUser.ID;
AdManager.AddAd(ad);
}
ViewBag.Json = Newtonsoft.Json.JsonConvert.SerializeObject(msgObject);
return View("result");
}
public ActionResult Dashboard()
{
return View();
}
public ActionResult ManageMessages()
{
List<AdManager.Ad> ads = AdManager.GetAdsForUser(CurrentUser.ID);
ViewBag.Ads = ads;
return View();
}
public ActionResult EditAd(int ad)
{
AdManager.Ad adv = AdManager.GetAd(ad);
ViewBag.Ad = adv;
return View();
}
[HttpPost]
public ActionResult UpdateAd(string ctatitle, string msgtitle, string msgbody, string imgurl,
string linkurl, string linktxt, string reply1, string reply2, string msgname,int adid)
{
ExpandoObject msgObject = CreateMessageObject(ctatitle, msgtitle, msgbody, imgurl, linkurl, linktxt, reply1, reply2, msgname);
AdManager.Ad ad = new AdManager.Ad();
ad.AdName = msgname;
ad.CallToAction = ctatitle;
ad.Image_URL = imgurl;
ad.LinkButtonText = linktxt;
ad.LinkButtonURL = linkurl;
ad.Reply1 = reply1;
ad.Reply2 = reply2;
ad.Subtitle = msgbody;
ad.Title = msgtitle;
ad.UserID = CurrentUser.ID;
ad.ID = adid;
AdManager.UpdateAd(ad);
ViewBag.Json = Newtonsoft.Json.JsonConvert.SerializeObject(msgObject);
return View("result");
}
public ActionResult UpdateAd(int Ad)
{
ViewBag.Ad= AdManager.GetAd(Ad);
return View();
}
public ActionResult Delete(int Ad)
{
ViewBag.DeletedAd = AdManager.DeleteAd(Ad);
List<AdManager.Ad> ads = AdManager.GetAdsForUser(CurrentUser.ID);
ViewBag.Ads = ads;
return View("ManageMessages");
}
public ActionResult Settings()
{
return View();
}
public ActionResult Help()
{
return View();
}
}
}
|