this is the error -----
ServerUrl/api/fileupload/uploadfile. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUplodaTest.aspx.cs" Inherits="UploadFileWebApi.FileUplodaTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
var ajaxRequest = $.ajax({
type: "POST",
url: "ServerUrl/api/fileupload/uploadfile",
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
alert("File Uploaded Sucessfull !!");
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<label for="fileUpload">
Select File to Upload:</label>
<input id="fileUpload" type="file" />
<input id="btnUploadFile" type="button" value="Upload File" />
</div>
</div>
</form>
</body>
</html>
and Web Api Controller Code is Here :----
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace UploadFileWebApi.Controllers
{
public class FileUploadController : ApiController
{
[HttpPost]
public void UploadFile()
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
string pathA = "D://Download";
if (httpPostedFile != null)
{
var fileSavePath = Path.Combine(pathA, httpPostedFile.FileName);
httpPostedFile.SaveAs(fileSavePath);
}
}
}
}
}
When i am Testing this web service on my local server this is work fine but when i host this web api on server and call it using jquery Ajax call(POST) FROM My consumer application this this error is showing in console .so please help me how to upload file from client to server and how to resolve this problem.
What I have tried:
When i am Testing this web service on my local server this is work fine but when i host this web api on server and call it using jquery Ajax call(POST) FROM My consumer application this this error is showing in console .so please help me how to upload file from client to server and how to resolve this problem.