2

I am trying to save attachments in ravenDb. I am getting a file not found error.

MVC View:

 <input type="file" name="file" id="Ids2" style="float:right"/> 

Over an ajax call, I am passing the value of the file name selected in the above control to the controller method - which in turns sends the file name to a custom method called "Upload"

public virtual string Upload(string fileName) { IDocumentSession session = GetCurrentDocumentSession(); var id = "upload/" + randomGen(); session.Advanced.DatabaseCommands.PutAttachment(id,null, File.ReadAllBytes(fileName), optionalMetaData); return id; } 

I am getting C:\ProgramFiles (x86)....does not have the file specified. Lets say in the view - I browsed to C:/Doc1.txt and clicked on Add button that saves bunch of other fields on the view and also picks up the file name/path from the file upload control.

I get an error at session.advance.databasecommands... line

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\Doc1.txt'.

If I manually move the Doc1.txt file to the above location, ravenDB saves the attachment and I can see it from localhost:8080/static/upload/keyvalue

How can I make ravenDB take the file from the location the user selects and not from the what it looks like a default location of c:programfiles.....

EDIT:

 function () { var iFile = iContainer.find( '#Ids2' ).val(); var DataToSave = { 'Attachment' : iFile }; var encodedData = $.toJSON(DataToSave); $.ajax({ type: 'POST' , url: '/AttController/Attach' , data: encodedData, contentType: 'application/json; charset=utf-8' , success: function (rc) { if (rc.Success) { // more javascript reroutes..business logic } else { alert(rc.Message); } }, error: function (xhr, ajaxOptions, thrownError) { alert( 'Error attaching \n' + xhr.response); } }); }; 
2
  • Can you provide the ajax? I think I know why but I want to be sure before answering Commented Jul 19, 2012 at 19:54
  • Thanks Shawn C.. please see OP for edit Commented Jul 19, 2012 at 20:16

1 Answer 1

1

Depending on the browser The html file control does not store the full path to the file. If you use Chrome and debug the script

var iFile = iContainer.find( '#Ids2' ).val(); 

Will return something like C:\fakepath\yourfile.txt. where as with IE the full path is returned.

Also you in your Ajax you are not pushing the bytes of the file but only the filename which means unless you are going to only ever run this website in a browser on the webserver the chances of the file being in the same place as the webserver is slim.

If you are trying to upload a file via ajax to a MVC controller I would suggest uploadify.

 $("#Ids2").uploadify( { uploader: '/AttController/Attach', swf: 'your/path/to/uploadify.swf', cancelImg: 'your/path/to/cancel.jpg', buttonText: 'Select File', fileSizeLimit: '300KB', fileTypeDesc: 'Image Files', fileTypeExts: '*.gif; *.jpg; *.png', auto: 'true', multiple: 'false', onError: function(type, info) { }, onUploadSuccess: function(file, data, response) { } }); 

Then just change your controller action to

public virtual ActionResult Upload(HttpPostedFileBase FileData) 

The FileData would have things like the FileName and would also have the file in an input stream.

Sign up to request clarification or add additional context in comments.

2 Comments

Lets say I am doing two things here.. one - saving bunch of fields and two -attaching .. from what you are saying, I need to do two separate ajax calls.. one for saving other fields, and other for just strictly uploading .. for simplicity in the above ajax code in OP, I removed all other fields that I am trying to save in addition to the upload..
No matter what you are doing you can not pass a string to a file on the client computer to a web server and have the web server find that file. You need to get the file bytes data and pass that to the server. This is not RavenDb related.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.