0

So I have the following method that I cannot alter:

    public async Task<User> UploadSaa(string filepath, User user)
    {

        Assembly cesCommon = Assembly.GetExecutingAssembly();
        byte[] saaBytes = null;
        using (Stream saarStream = cesCommon.GetManifestResourceStream(name))
        using (MemoryStream saaMemoryStream = new MemoryStream())
        {
            saaStream.CopyTo(saarMemoryStream);
            saaBytes = saaMemoryStream.ToArray();
            user.Saa = saaBytes;
        }
        user = await SaveNewUser(user);
        return user;
    }

In previous usages of it, filepath was passed directly to it in order to initialize the database for testing. However, now I need to find a way to pass a variable string to UploadSaa(), b/c in production, the user will be selecting their own file from their own system, and I can't dictate the filepath to them. I tried to use OpenFileDialog, but that returns a file, not a path

My questions are: How can I modify the OpenFileDialog to accept a path to a file, which can then be passed to UploadSaa? Is there a better option? If I must modify UploadSaa, what should the changes be?

Will
  • 103
  • 12
  • 1
    AFAIK, full file path can not passed from browser due to security reason. I am in confused, why do you want to get user's local file path? – Akash KC Feb 15 '17 at 19:39
  • So that the user can upload a pdf file to the database. – Will Feb 15 '17 at 19:42
  • I've posted an answer for uploading file in asp.net mvc – Akash KC Feb 15 '17 at 20:00
  • Thank you everybody for all the responses! You've all given some wonderful avenues to pursue, and I'll be accepting an answer within the next couple of hours after I see what works best! – Will Feb 15 '17 at 20:12

3 Answers3

0

You should be able to get the full path from openFileDialog object.FileName

OpenFileDialog object= new OpenFileDialog();
string fullPath = object.FileName;

Or

You can also look into other options like using FolderBrowserDialog.

Refer to this example.

I hope that helps!

Community
  • 1
  • 1
CodeNinja
  • 26
  • 7
0

Your question is not clear, actually you posted the tag using asp.net mvc and you are using the OpenFileDialog? is this related with Windows Forms, or with a web application? If it is the latter check below

Try to use an HttpPostedFileBase, from there you can get the path

https://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.filename(v=vs.110).aspx

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{...}

create the ModelBinder

public class HttpOdometerPostedFileBaseModelBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if(controllerContext == null)
                throw new ArgumentNullException(nameof(controllerContext));

            if(bindingContext == null)
                throw new ArgumentNullException(nameof(bindingContext));

            HttpPostedFileBase theFile = controllerContext.HttpContext.Request.Files[bindingContext.ModelName];

            return theFile;
        }
    }

and register it on the global.asax.cs

System.Web.Mvc.ModelBinders.Binders[typeof(HttpPostedFileBase)] = new HttpOdometerPostedFileBaseModelBinder();
Zinov
  • 3,817
  • 5
  • 36
  • 70
0

I suspect by looking at your code, you are assuming getting file in web as similar to desktop version. In desktop version, you can get full path and read stream accordingly. But in web, browser will pass stream (for file type input) instead of passing filepath. So, you just need to have action which can catch your file stream.

You can do with HttpPostedFileBase and it'll gives you stream, filename and contentlength.

You action method should be like this :

[HttpPost]  
public ActionResult MyUploadingAction(HttpPostedFileBase file)  
{  
    var filename = file.FileName; 
    // For reading stream
    var reader = new BinaryReader(file.InputStream);
    byte[] byteData= reader.ReadBytes(file.InputStream.Length);


    return View();  
}
Akash KC
  • 16,057
  • 6
  • 39
  • 59