Wrote a post in Wiki Ninjas - Official Blog of TechNet Wiki. The title of the post is Wiki Life: Wikis vs. Blog Posts.
| Wiki Life: Wikis vs. Blog Posts |
Wiki Life: Wikis vs. Blog Posts
Happy Coding.
Regards,
Jaliya
| Wiki Life: Wikis vs. Blog Posts |
| Visual C# Technical Guru - April 2016 |
.state("app.somestate.someotherstate", {
url: "/someotherstate/:myobjectparameter",
templateUrl: "sometemplate.tpl.html",
controller: "SomeTemlateController",
})
$state.go("app.somestate.someotherstate", {
"myobjectparameter": JSON.stringify({
"property1": "Property Value1",
"property2": "Property Value2"
})
});
var myObjectParameter = JSON.parse($stateParams.myobjectparameter);
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Hello World! \n");
await next.Invoke();
});
app.Map("/contactus", HandleContactUs);
private static void HandleContactUs(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync("Contact Us");
});
}
app.MapWhen(context =>
{
return context.Request.Query.ContainsKey("pid");
}, HandleProductDetail);
private static void HandleProductDetail(IApplicationBuilder app)
{
app.Run(async context =>
{
await context.Response.WriteAsync("Product Details");
});
}
app.Run(async context =>
{
await context.Response.WriteAsync("Main Content");
});
[HttpGet]
[Route("values/download")]
public HttpResponseMessage Download(string name)
{
try
{
string fileName = string.Empty;
if (name.Equals("pdf", StringComparison.InvariantCultureIgnoreCase))
{
fileName = "SamplePdf.pdf";
}
else if (name.Equals("zip", StringComparison.InvariantCultureIgnoreCase))
{
fileName = "SampleZip.zip";
}
if (!string.IsNullOrEmpty(fileName))
{
string filePath = HttpContext.Current.Server.MapPath("~/App_Data/") + fileName;
using (MemoryStream ms = new MemoryStream())
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray());
httpResponseMessage.Content.Headers.Add("x-filename", fileName);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
}
}
return this.Request.CreateResponse(HttpStatusCode.NotFound, "File not found.");
}
catch (Exception ex)
{
return this.Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
}
}
$scope.downloadFile = function (name) {
$http({
method: 'GET',
url: 'api/values/download',
params: { name: name },
}).success(function (data, status, headers) {
}).error(function (data) {
});
};
$scope.downloadFile = function (name) {
$http({
method: 'GET',
url: 'api/values/download',
params: { name: name },
responseType: 'arraybuffer'
}).success(function (data, status, headers) {
headers = headers();
var filename = headers['x-filename'];
var contentType = headers['content-type'];
var linkElement = document.createElement('a');
try {
var blob = new Blob([data], { type: contentType });
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", filename);
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
} catch (ex) {
console.log(ex);
}
}).error(function (data) {
console.log(data);
});
};
| Download |
| Download |
public static void Add(this MultipartFormDataContent form, HttpContent content, string name, string fileName, object headerValues)
{
var header = new ContentDispositionHeaderValue("form-data")
{
Name = name,
FileName = fileName
};
var headerParameters = new HttpRouteValueDictionary(headerValues);
foreach (var parameter in headerParameters)
{
header.Parameters.Add(new NameValueHeaderValue(parameter.Key, parameter.Value.ToString()));
}
content.Headers.ContentDisposition = header;
form.Add(content);
}
using (var multipartFormDataContent = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture)))
{
multipartFormDataContent.Add(new StreamContent(new MemoryStream(data)), name, fileName, new
{
Parameter1="HelloWorld"
});
}
MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
Func<ICollection<NameValueHeaderValue>, string, string> GetHeaderValueByKey = (headerValues, key) =>
{
if (headerValues == null)
return null;
NameValueHeaderValue nameValueHeaderValue = headerValues.FirstOrDefault(x => x.Name.Equals(key, StringComparison.OrdinalIgnoreCase));
return nameValueHeaderValue != null ? nameValueHeaderValue.Value : null;
};
HttpContent file = provider.Contents.FirstOrDefault();
string parameter1Value = GetHeaderValueByKey(file.Headers.ContentDisposition.Parameters, "Parameter1");
| Result |