0
sampath created
Hi, I have developed below mentioned code snippets for the app with the WebApi (not for the ABP app).So my question is how can I replace Application Layer method instaed of the Web api Url below (i.e. url: "/api/EventImages/AddPictures",) ? Thanks.
Note : In other words how can I implement below kind of implementation with the ABP.
JS
//to add Pictures
vm.AddPictures = function ($files) {
vm.upload = [];
for (var i = 0; i < $files.length; i++) {
var $file = $files_;
(function (index) {
vm.upload[index] = $upload.upload({
url: "/api/EventImages/AddPictures",
method: "POST",
data: {},
file: $file
}).progress(function (evt) {
}).success(function (data, status, headers, config) {
}).error(function (data, status, headers, config) {
});
})(i);
}
};
[i:2h7l3lfn]Web Api method which above app has been consumed_
[HttpPost]
public async Task<HttpResponseMessage> AddPictures()
{
if (!Request.Content.IsMimeMultipartContent())
{
this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
var newImageName = string.Empty;
var path = System.Web.Hosting.HostingEnvironment.MapPath("~");
var provider = GetMultipartProvider();
await Request.Content.ReadAsMultipartAsync(provider);
foreach (var r in provider.FileData)
{
var uploadedFileInfo = new FileInfo(r.LocalFileName);
var uploadedFileName = uploadedFileInfo.Name;
var originalFileName = GetDeserializedFileName(r);
var extension = Path.GetExtension(originalFileName);
if (extension == null) continue;
var ext = extension.ToLower();
var guid = Guid.NewGuid().ToString();
newImageName = guid + ext;
try
{
File.Move(path + "\\Resources\\images\\temp\\" + uploadedFileName, path + "\\Resources\\images\\event-images\\" + newImageName);
}
catch (IOException ex)
{
//Write error
}
}
return Request.CreateResponse(HttpStatusCode.OK, new { newImageName });
}
2 Answer(s)
-
0
Hi,
I suggest you to create regular web api controller, instead of application service method. Because, your code seems web related and should be in the web layer.
-
0
Hi, Perfect it works.I have created a new webApi controller on the WebApi Layer as PictureController .Thanks a lot :)
Here is the complete code snippet.
public class PictureController : IPApiControllerBase { /// <summary> /// to Add Pictures /// </summary> [HttpPost] public async Task<HttpResponseMessage> AddPictures() { if (!Request.Content.IsMimeMultipartContent()) { this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType); } var newImageName = string.Empty; var path = System.Web.Hosting.HostingEnvironment.MapPath("~"); var provider = GetMultipartProvider(); await Request.Content.ReadAsMultipartAsync(provider); foreach (var r in provider.FileData) { var uploadedFileInfo = new FileInfo(r.LocalFileName); var uploadedFileName = uploadedFileInfo.Name; var originalFileName = GetDeserializedFileName(r); var extension = Path.GetExtension(originalFileName); if (extension == null) continue; var ext = extension.ToLower(); var guid = Guid.NewGuid().ToString(); newImageName = guid + ext; try { File.Move(path + "\\Common\\temp\\" + uploadedFileName, path + "\\Common\\pictures\\" + newImageName); } catch (IOException ex) { //Write error } } return Request.CreateResponse(HttpStatusCode.OK, new { newImageName }); } #region Private Methods /// <summary> /// to Get Multi part Provider /// </summary> private MultipartFormDataStreamProvider GetMultipartProvider() { const string uploadFolder = "~/Common/temp"; var root = HttpContext.Current.Server.MapPath(uploadFolder); Directory.CreateDirectory(root); return new MultipartFormDataStreamProvider(root); } /// <summary> /// to Get De-serialized File Name /// </summary> private string GetDeserializedFileName(MultipartFileData fileData) { var fileName = GetFileName(fileData); return JsonConvert.DeserializeObject(fileName).ToString(); } /// <summary> /// to Get File Name /// </summary> private string GetFileName(MultipartFileData fileData) { return fileData.Headers.ContentDisposition.FileName; } #endregion Private Methods }