Parcourir la source

Downloading files.

master
radivoje.milutinovic il y a 3 ans
Parent
révision
30faa152fd

+ 18
- 4
SecureSharing/Controllers/HomeController.cs Voir le fichier

using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using SecureSharing.Business.Dtos; using SecureSharing.Business.Dtos;
using SecureSharing.Business.Interfaces; using SecureSharing.Business.Interfaces;
using SecureSharing.Data.Data; using SecureSharing.Data.Data;
private readonly IMessageService _messageService; private readonly IMessageService _messageService;
private readonly IModelFactory _modelFactory; private readonly IModelFactory _modelFactory;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
private const string DefaultPath = "files";
public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment) public HomeController(ILogger<HomeController> logger, IMessageService messageService, IModelFactory modelFactory, IWebHostEnvironment webHostEnvironment)
{ {
var message = new MessageDto { Text = model.Text }; var message = new MessageDto { Text = model.Text };
// var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
var basePath = $"C:\\Users\\radivoje.milutinovic\\Downloads\\fajlovitmp\\{message.Code}";
var basePath = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, message.Code.ToString());
// var basePath = $"C:\\Users\\radivoje.milutinovic\\Downloads\\fajlovitmp\\{message.Code}";
Directory.CreateDirectory(basePath); Directory.CreateDirectory(basePath);
foreach (var formFile in model.Files) foreach (var formFile in model.Files)
{ {
if (formFile.Length <= 0) continue; if (formFile.Length <= 0) continue;
return RedirectToAction("Link", "Home", new { code = code, share = true }); return RedirectToAction("Link", "Home", new { code = code, share = true });
} }


public async Task<FileStreamResult> Download(string filename, Guid code)
{
var path = Path.Combine(_webHostEnvironment.WebRootPath.Split('/')[0], DefaultPath, code.ToString(), filename);
var memory = new MemoryStream();
await using var stream = new FileStream(path, FileMode.Open);
await stream.CopyToAsync(memory);
memory.Position = 0;
string? contentType;
new FileExtensionContentTypeProvider().TryGetContentType(filename, out contentType);
return contentType is null ? null : File(memory, contentType,Path.GetFileName(path));
// Response.Redirect();
}

[HttpGet] [HttpGet]
public async Task<IActionResult> Link(Guid code, bool? share) public async Task<IActionResult> Link(Guid code, bool? share)
{ {

+ 0
- 1
SecureSharing/Models/LinkModel.cs Voir le fichier

public MessageModel MessageModel { get; set; } public MessageModel MessageModel { get; set; }
public bool? Share { get; set; } public bool? Share { get; set; }
public TimeSpan? TimeLeft { get; set; } public TimeSpan? TimeLeft { get; set; }

public bool IsValid { get; set; } public bool IsValid { get; set; }
} }

+ 3
- 4
SecureSharing/Views/Home/Link.cshtml Voir le fichier

<div class="label-text link-show"> <div class="label-text link-show">
@foreach (var file in Model.MessageModel.FileNames) @foreach (var file in Model.MessageModel.FileNames)
{ {
<p>@file</p>
<a asp-action="Download" asp-route-filename="@file" asp-route-code="@Model.MessageModel.Code">@file</a>
} }
</div> </div>
} }
<div class="label-text link-show"> <div class="label-text link-show">
@foreach (var file in Model.MessageModel.FileNames) @foreach (var file in Model.MessageModel.FileNames)
{ {
<p>@file</p>
<a asp-action="Download" asp-route-filename="@file" asp-route-code="@Model.MessageModel.Code">@file</a>
} }
</div> </div>
} }
} }


<script> <script>
function copyToClipboard() {
copyToClipboard = () => {
// Get the text field // Get the text field
const copyText = document.getElementById("a-link").innerText; const copyText = document.getElementById("a-link").innerText;
// Copy the text inside the text field // Copy the text inside the text field
navigator.clipboard.writeText(copyText); navigator.clipboard.writeText(copyText);

} }
</script> </script>

Chargement…
Annuler
Enregistrer