Skip to content

Commit aee6a9c

Browse files
dimodiDimo Dimov
authored andcommitted
docs: Update Upload examples (#753)
* docs: Update Upload examples * use WASM code in Upload examples Co-authored-by: Dimo Dimov <[email protected]>
1 parent 17d1045 commit aee6a9c

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

components/upload/events.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,11 @@ namespace MyBlazorApp.Controllers
225225
{
226226
var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
227227
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
228-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
228+
229+
// server Blazor app
230+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
231+
// client Blazor app
232+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
229233
230234
string customHeaderValue = Request.Headers["CustomHeader"]; // the key from the OnUpload event
231235
string customFormValue = Request.Form["SomeFormField"]; // the key from the OnUpload event
@@ -355,7 +359,11 @@ namespace MyBlazorApp.Controllers
355359
// implement security and validation here
356360
357361
var fileName = Path.GetFileName(fullName);
358-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
362+
363+
// server Blazor app
364+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
365+
// client Blazor app
366+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
359367
360368
if (System.IO.File.Exists(physicalPath))
361369
{
@@ -504,7 +512,11 @@ namespace MyBlazorApp.Controllers
504512
{
505513
var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
506514
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
507-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
515+
516+
// server Blazor app
517+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
518+
// client Blazor app
519+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
508520
509521
// implement security and validation here
510522
@@ -624,7 +636,11 @@ namespace MyBlazorApp.Controllers
624636
{
625637
var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
626638
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
627-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
639+
640+
// server Blazor app
641+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
642+
// client Blazor app
643+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
628644
629645
// always cause an exception to showcase the idea
630646
// these are examples of throwing errors and sending response texts and status code
@@ -760,4 +776,4 @@ The `OnClear` event fires when the user clicks the Clear button which is availab
760776
* [Upload Overview]({%slug upload-overview%})
761777
* [Validation]({%slug upload-validation%})
762778
* [Upload Methods]({%slug upload-overview%}#methods)
763-
* [Live Demo: Upload Events](https://demos.telerik.com/blazor-ui/upload/events)
779+
* [Live Demo: Upload Events](https://demos.telerik.com/blazor-ui/upload/events)

components/upload/overview.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,20 @@ The <a href="https://www.telerik.com/blazor-ui/upload" target="_blank">Blazor Up
6969
try
7070
{
7171
var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
72-
72+
7373
// Some browsers send file names with full path.
7474
// We are only interested in the file name.
7575
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
76+
77+
// server Blazor app
7678
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
77-
79+
// client Blazor app
80+
//var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
81+
7882
// Implement security mechanisms here - prevent path traversals,
7983
// check for allowed extensions, types, size, content, viruses, etc.
8084
// This sample always saves the file to the root and is not sufficient for a real application.
81-
85+
8286
using (var fileStream = new FileStream(physicalPath, FileMode.Create))
8387
{
8488
await file.CopyToAsync(fileStream);
@@ -91,12 +95,11 @@ The <a href="https://www.telerik.com/blazor-ui/upload" target="_blank">Blazor Up
9195
await Response.WriteAsync("some error message"); // custom error message
9296
}
9397
}
94-
98+
9599
// Return an empty string message in this case
96100
return new EmptyResult();
97101
}
98-
99-
102+
100103
[HttpPost]
101104
public ActionResult Remove(string fileToRemove) // must match RemoveField which defaults to "files"
102105
{
@@ -105,11 +108,12 @@ The <a href="https://www.telerik.com/blazor-ui/upload" target="_blank">Blazor Up
105108
try
106109
{
107110
var fileName = Path.GetFileName(fileToRemove);
111+
108112
// server Blazor app
109113
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
110114
// client Blazor app
111115
//var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
112-
116+
113117
if (System.IO.File.Exists(physicalPath))
114118
{
115119
// Implement security mechanisms here - prevent path traversals,
@@ -126,7 +130,7 @@ The <a href="https://www.telerik.com/blazor-ui/upload" target="_blank">Blazor Up
126130
Response.WriteAsync("some error message"); // custom error message
127131
}
128132
}
129-
133+
130134
// Return an empty string message in this case
131135
return new EmptyResult();
132136
}
@@ -258,8 +262,12 @@ The Upload methods are accesible through its [reference](#upload-reference).
258262
// Some browsers send file names with full path.
259263
// We are only interested in the file name.
260264
var fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
261-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
262-
265+
266+
// server Blazor app
267+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
268+
// client Blazor app
269+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
270+
263271
// Implement security mechanisms here - prevent path traversals,
264272
// check for allowed extensions, types, size, content, viruses, etc.
265273
// This sample always saves the file to the root and is not sufficient for a real application.
@@ -276,12 +284,11 @@ The Upload methods are accesible through its [reference](#upload-reference).
276284
await Response.WriteAsync("some error message"); // custom error message
277285
}
278286
}
279-
287+
280288
// Return an empty string message in this case
281289
return new EmptyResult();
282290
}
283-
284-
291+
285292
[HttpPost]
286293
public ActionResult Remove(string fileToRemove) // must match RemoveField which defaults to "files"
287294
{
@@ -290,17 +297,18 @@ The Upload methods are accesible through its [reference](#upload-reference).
290297
try
291298
{
292299
var fileName = Path.GetFileName(fileToRemove);
300+
293301
// server Blazor app
294-
var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
302+
//var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, fileName);
295303
// client Blazor app
296-
//var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
304+
var physicalPath = Path.Combine(HostingEnvironment.ContentRootPath, fileName);
297305
298306
if (System.IO.File.Exists(physicalPath))
299307
{
300308
// Implement security mechanisms here - prevent path traversals,
301309
// check for allowed extensions, types, permissions, etc.
302310
// this sample always deletes the file from the root and is not sufficient for a real application.
303-
311+
304312
System.IO.File.Delete(physicalPath);
305313
}
306314
}
@@ -311,7 +319,7 @@ The Upload methods are accesible through its [reference](#upload-reference).
311319
Response.WriteAsync("some error message"); // custom error message
312320
}
313321
}
314-
322+
315323
// Return an empty string message in this case
316324
return new EmptyResult();
317325
}
@@ -347,4 +355,3 @@ Cross-origin requests depend on the application and endpoint setup. The `WidthCr
347355
* [Events]({%slug upload-events%})
348356
* [Validation]({%slug upload-validation%})
349357
* [Live Demo: Upload](https://demos.telerik.com/blazor-ui/upload/overview)
350-

0 commit comments

Comments
 (0)