Consumir un WEBApi C# + Jquery
Publicado por orpare (1 intervención) el 31/01/2020 16:23:49
Cordial saludo,
Tengo el siguiente codigo para inportar un Archivo...
Pero me genera el error 404 al invocar...
La pregunta es como hago la llamada al api?
Tengo el siguiente codigo para inportar un Archivo...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class FileController : ApiController
{
[HttpPost] // This is from System.Web.Http, and not from System.Web.Mvc
public async Task<HttpResponseMessage> Upload()
{
if (!Request.Content.IsMimeMultipartContent())
{
this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
var provider = GetMultipartProvider();
var result = await Request.Content.ReadAsMultipartAsync(provider);
// On upload, files are given a generic name like "BodyPart_26d6abe1-3ae1-416a-9429-b35f15e6e5d5"
// so this is how you can get the original file name
var originalFileName = GetDeserializedFileName(result.FileData.First());
// uploadedFileInfo object will give you some additional stuff like file length,
// creation time, directory name, a few filesystem methods etc..
var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);
// Remove this line as well as GetFormData method if you're not
// sending any form data with your upload request
var fileUploadObj = GetFormData<InfoArchivo>(result);
// Through the request response you can return an object to the Angular controller
// You will be able to access this in the .success callback through its data attribute
// If you want to send something to the .error callback, use the HttpStatusCode.BadRequest instead
var returnData = "ReturnTest";
return this.Request.CreateResponse(HttpStatusCode.OK, new { returnData });
}
// You could extract these two private methods to a separate utility class since
// they do not really belong to a controller class but that is up to you
private MultipartFormDataStreamProvider GetMultipartProvider()
{
// IMPORTANT: replace "(tilde)" with the real tilde character
// (our editor doesn't allow it, so I just wrote "(tilde)" instead)
var uploadFolder = "'/App_Data/Tmp/FileUploads"; // you could put this to web.config
var root = HttpContext.Current.Server.MapPath(uploadFolder);
Directory.CreateDirectory(root);
return new MultipartFormDataStreamProvider(root);
}
// Extracts Request FormatData as a strongly typed model
private object GetFormData<T>(MultipartFormDataStreamProvider result)
{
if (result.FormData.HasKeys())
{
var unescapedFormData = Uri.UnescapeDataString(result.FormData
.GetValues(0).FirstOrDefault() ?? String.Empty);
if (!String.IsNullOrEmpty(unescapedFormData))
return JsonConvert.DeserializeObject<T>(unescapedFormData);
}
return null;
}
private string GetDeserializedFileName(MultipartFileData fileData)
{
var fileName = GetFileName(fileData);
return JsonConvert.DeserializeObject(fileName).ToString();
}
public string GetFileName(MultipartFileData fileData)
{
return fileData.Headers.ContentDisposition.FileName;
}
////////////////////////////////
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
url: "/api/File",
data: data,
processData: false,
contentType: false,
cache: false,
timeout: 600000,
success: function(data) {
console.log("SUCCESS : ", data);
$("#BtnImportar").prop("disabled", false);
}
});
Pero me genera el error 404 al invocar...
La pregunta es como hago la llamada al api?
Valora esta pregunta


0